The Wayback Machine - https://web.archive.org/web/20240910091231/https://www.geeksforgeeks.org/loop-construct-in-lisp/
Open In App

Loop Construct in LISP

Last Updated : 22 Nov, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will discuss Loop Construct. This Construct is used to iterate the data until it finds the return statement. And then it will stop iterating and return the results.

Syntax:

(loop (statements)
condition
return 
)

where,

  • loop is the keyword
  • statements are used to iterate the loop
  • condition is used to specify the condition so that the loop stops iterating
  • return statement is used to return the results

Example: LISP Program to iterate over elements

Lisp




;define a variable and set to 1
(setq var 1)
  
;start the loop
(loop 
   
;increment value by 2 each time
;till value is less than 30
   (setq var (+ var 2))
   
   ;display
   (write var)
   (terpri)
   
   ;condition for value is less than 30
   (when (> var 30) (return var))
)


Output:

3
5
7
9
11
13
15
17
19
21
23
25
27
29
31

Example 2:

Lisp




;define a variable and set to 100
(setq var 100)
  
;start the loop
(loop 
   
;decrement value by 10 each time
;till value is less than 30
   (setq var (- var 10))
   ;display
   
   (write var)
   (terpri)
   
   ;condition for value is less than 30
   (when (< var 30) (return var))
)


Output:

90
80
70
60
50
40
30
20


Similar Reads

Loop For Construct in LISP
The loop for construct in common LISP is used to iterate over an iterable, similar to the for loop in other programming languages. It can be used for the following: This is used to set up variables for iteration.It can be used for conditionally terminate the iteration.It can be used for operating on the iterated elements. The loop For construct can
2 min read
Dotimes Construct in LISP
In this article, we will discuss the dotimes loop in LISP. The dotimes is a looping statement used to iterate the elements. Unlike other looping constructs, it only loops for a specified number of iterations. Syntax: (dotimes (n range) statements --------------- -------------- ) where, n is the starting number-0The range is the last number till tha
1 min read
Dolist Construct in LISP
DoList in Common LISP is a looping statement used to iterate the elements in a list. Syntax: (dolist input_list) statements... ) Here, The input_list contains the list of elements that are iterated.The statements are present in the loop. Example 1: LISP Program to iterate the list of elements from 1 to 5. C/C++ Code ;create a dolist of 1 to 5 eleme
2 min read
Cond Construct in LISP
In this article, we will discuss cond construct in LISP. The cond is a decision-making statement used to make n number of test conditions. It will check all the conditions. Syntax: (cond (condition1 statements) (condition2 statements) (condition3 statements) ... (conditionn statements) ) Here, The conditions specify different conditions - if condit
2 min read
If Construct in LISP
In this article, we will discuss the if construct in LISP. The if is a decision-making statement used to check whether the condition is right or wrong. The if the condition is right, then it will go inside the if block and execute the statements under if block. Otherwise, the statements are not executed. Syntax: (if (condition) then (statement 1)..
2 min read
Do Construct in LISP
In this article, we will discuss the Do Construct in LISP. Do construct is used to perform an iteration in a structured format. Syntax: (do ((variable1 value1 new updated value 1) (variable2 value2 new updated value 2) ------------------------------- ------------------------------- (variable n value n new updated value n) ...) (test statements) ) H
2 min read
When Construct in LISP
In this article, we will discuss the when construct. The when is a decision-making statement used to specify the decisions. It is similar to conditional statements. Syntax: (when (condition) (statements) ) where, condition is a test statement used to teststatements are the actions that will depend on the condition Example 1: LISP Program to check t
2 min read
Case Construct in LISP
In this article, we will discuss the case construct in LISP. This is used to check multiple test conditions at a time, unlike cond, if and when it allows multiple conditions. Syntax: (case (key_value) ((key1) (statement 1 .............. statement n) ) ((key2) (statement 1 .............. statement n) ) ................ ((keyn) (statement 1 .........
2 min read
Constants in LISP
In LISP all constants are global variables. Values of constant never change throughout the execution of the program. Defining constant in LISP: New global constants are defined using the DEFCONSTANT construct Syntax: (defconstant name initial-value-form "documentation-string") Example: Let's create a global constant whose value will contain the rat
2 min read
LISP - Comparison Operators on Characters & Strings
The contents of a field are compared to the contents of another field or a constant using Comparison operators. In Simple words, comparator operators are used to compare whether the two or more different values are equal or not. Comparison Operators on Character: Characters are not supported by numerical comparison functions and operators such as
3 min read
Logical Operators in LISP
Common LISP supports 3 types of logical operators on Boolean Values. The arguments of these operators are evaluated conditionally, therefore they are also part of the LISP control Structure. The common LISP operators are listed below in the table: OperatorSyntaxDescriptionandand number1 number2This operator takes two numbers which are evaluated lef
2 min read
Input & Output in LISP
Pre-requisites: Introduction to LISP Lisp provides a huge set of facilities for performing input/output. All the input/output operations are performed on streams of several kinds. While reading and writing binary data is possible, the majority of Common Lisp input/output methods read or write characters. For reading and writing individual character
10 min read
Keyword Parameters in LISP
LISP Function's parameters list has a basic aim of declaring the variables which will receive the arguments that are passed in the function. Normally parameter list contains a basic list of variable names, those parameters are called "required parameters". If a function is called, for every required parameter it should be supplied with one individu
3 min read
Basic Syntax in LISP
LISP is a list processing programming language. It is widely used in the manipulation of data strings. It provides an input and output library. LISP provides a macro system and provides well control structures for the manipulation of data. Basic Blocks in LISP:There are three basic building blocks of the LISP program: AtomListStringAtom: An atom in
2 min read
Program Structure in LISP
The expressions we use in Lisp are known as the s-expressions which are composed of atoms, strings, and lists. An s-expression is itself a valid program. REPL:REPL is an acronym for Read Evaluate Print Loop. In the case of an interpreter, the program is read, checked for errors in loops, and the value is returned by the program. Lisp program can ru
2 min read
Strings in LISP
A string is a set of characters. String are enclosed in double-quotes. Example: "hello geek","java","python" etc Example: LISP program to display strings C/C++ Code ;edisplay hello geek (write-line &quot;Hello Geek&quot;) ;display (write-line &quot;Welcome to java&quot;) Output: Hello Geek Welcome to javaString Comparison Functions: Used to compare
4 min read
Comparison Operators in LISP
In this article, we will discuss the comparison operators in LISP. These operators are used to compare numbers by taking two or more operands. Note: This will work only on numbers, Different comparison operators are:OperatorSyntaxNameDescription== operand1 operand2equal toThis operator checks if the values of the operands are all equal or not, if y
4 min read
Hash Table in LISP
A hash table is a type of collection in Common LISP, that is used to map keys to values. Any non-null object can be used as a key or as a value. To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method. Types of Hash Tables in LISP: There are three types of hashta
3 min read
Arithmetic Operators in LISP
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, and division. There are 7 arithmetic operators in LISP that are listed in the below table: OperatorSyntaxDescriptionAddition Operator(+)+ num1 num2Add the two numbersSubtraction Operator(-)- num1 num2Subtract the second number from the first
2 min read
Lists in LISP
Lists in common LISP is simply a single Linked list. In LISP, Lists are designed as a chain of records. While talking about record structures in LISP, the concept of Cons is vital. Cons in LISP is a record structure with 2 primary components. A cons function takes in 2 arguments and returns a new cons cell with car and dir. car: It is used to acces
2 min read
Loops in LISP
Loops allow executing a set of instructions repeatedly while some condition is true. LISP provides the following types of loops: 1. dotimes loop: The dotimes loop allows executing instructions for a fixed number of times.Syntax: (dotimes( variableName numberOfIterations ) ( expressions )) Where, variableName =&gt; Name of the variable, this can be
4 min read
Functions in LISP
A function is a set of statements that takes some input, performs some tasks, and produces the result. Through functions, we can split up a huge task into many smaller functions. They also help in avoiding the repetition of code as we can call the same function for different inputs. Defining Functions in LISP: Functions in LISP are defined using th
3 min read
Variables in LISP
Similar to other languages, in LISP variables are named places that store a particular value, but they are not declared the way you declare variables in C++ or Java i.e you don't need to mention data-type of variables when declaring them as LISP is dynamically typed. LISP supports two types of variables: Local variablesGlobal variablesLocal Variabl
3 min read
Optional Parameters in LISP
Optional Parameters are the parameters that are optional in the function. We can put optional parameters whenever the arguments are not necessary. If we keep the optional parameters in a function and pass the values, then the values are taken in place of optional parameters. If values are not passed, then optional parameters will take NIL and retur
2 min read
How to Install LISP on MacOS?
Lisp is one of the oldest programming languages. It was invented back in the 1950s by John McCarthy. Lisp is a so-called meta-programming language or a symbolic programming language. It can be used for any sort of task. It's a general-purpose language. It is the second-oldest high-level programming language. In this tutorial, we are going to see ho
1 min read
Macros in LISP
Macro is similar to a function in popular languages like C++, java, etc. that takes arguments and returns a LISP form to be evaluated. It is useful when the same code has to be executed with a few variable changes.For example, rather than writing the same code for squaring a number, we can define a macro that holds the code for squaring. Defining a
2 min read
Rest Parameters in LISP
LISP Function's parameters list has a basic aim of declaring the variables which will receive the arguments that are passed in the function. Normally parameter list contains a basic list of variable names, those parameters are called "required parameters". If a function is called, for every required parameter it should be supplied with one individu
3 min read
Returning Values Functions in LISP
In the previous article on the LISP function, we have seen the syntax of defining a function and calling the function by passing some arguments. Returning values from a function: In LISP, whatever is the last expression in the body of a function becomes the return value of that function. Example: Let's create a function that takes the input paramet
2 min read
Operators in LISP
Operators are the foundation of any programming language. Thus the functionality of the LISP programming language is incomplete without the use of operators. We can define operators as symbols that help us to perform specific mathematical and logical computations on operands. In other words, we can say that an operator operates the operands. Differ
5 min read
Bitwise Operators in LISP
In this article, we will discuss the Bitwise operators in LISP. These operators are used to perform the manipulation of individual bits of a number. The truth table for bitwise AND, NAND, OR, XOR, NOR, &amp; XNOR: aba and b a nand ba or b a xor b a nor b a xnor b00010011010111001110100110011100 Different bitwise operators in LISP are listed below i
4 min read
Article Tags :