Loops in LISP - GeeksforGeeks

06 Oct, 2021
Loops allow executing a set of instructions repeatedly while some condition is true. LISP provides the following types of loops:
The dotimes loop allows executing instructions for a fixed number of times.Syntax: 
(dotimes( variableName numberOfIterations ) (
  expressions 
))
Where,
variableName => Name of the variable, this can be used to get current iteration number
numberOfIterations => Total number of iterations
expressions => Statements to be executed in each iteration
Example:
Lisp
(dotimes (a 4)  (print a))(write-line "")
Output:
You can also return from the loop if a certain condition is met
Lisp
(dotimes (i 7)   (if (> i 5)       (return)       (print i))    )(write-line "")
Here, the loop is set to iterate 7 times, however, when the value becomes more than 5, the loop stops because the condition is met. Output:
The loop construct allows executing some statement(s) repeatedly until it finds a return statement.
Syntax: 
( loop   
  (expressions)
  ( when (condition) (return returnValue))
)
Where,
expressions => Statements to be executed in each iteration
condition => Condition that specifies when to exit loop
returnValue => Value to be returned
Example:
Lisp
(defvar n 3)(loop    (print n)    (setq n (+ n 1))    (when (> n 6) (return n)))(write-line "")
Here, n is printed and incremented until it becomes greater than 6.Output:
The loop for construct is similar to the for-loops in popular languages like java, c++, etc. It can also be used to traverse lists and other data structures.
Syntax:
( loop for variableName from startValue to endValue by changeValue do
   (expressions)
)
Where,
variableName => Name of the variable, this can be used to get the value of variable in current iteration. 
startValue => Initial value of variable
endValue => End value of variable (after last iteration)
changeValue => Specifies by how much to increment or decrement value after every iteration
expressions => Statements to be executed in each iteration
Example:
Lisp
; initialize x=12, execute instructions, ; increment it by 3 and perform iteration until it is less than 25(loop for x from 12 to 25 by 3 do    (print x))(write-line "")
Output:
The do construct allows a structured form of iteration.Syntax:
(do ((variable_1    value_1   updated-value_1)
      (variable_2   value_2   updated-value_2)
      (variable_3   value_3   updated-value_3)
   ...)
   (test return_value)
   (s-expressions)
)
Where,
variable_1 => First variable
value_1 => Initial Value for first variable
updated-value_1 => Specifies how to update variable_1 after each iteration.
variable_2 => Second variable
value_2 => Initial Value for second variable
updated-value_2 => Specifies how to update variable_2 after each iteration. ... similar till variable_n
test => Condition that specifies when to stop the loop
return value => Value that is returned at the end of iteration
expressions => Other statements to be executed in every iteration
The initial values of each variable are evaluated and bound to the respective variable. The updated value in every clause is with respect to an optional update statement which indicates how to update variables in each iteration.The test is performed on each iteration and if the test expression results in a non-null or true value, the returned value is evaluated and returned.If the last optional s-expression(s) are specified, they’re executed on every iteration while test expression results are true.Example:
Lisp
(do ((a 0 (+ 2 a))   (b 20 ( - b 2)))   ((= a b)(- a b))   (format t "~% a = ~d  b = ~d" a b))(write-line "")
Output:
The dolist construct provides an easy way to traverse lists.
Syntax:
(dolist (listItemVariable list)
   (expressions)
) 
Where,
listItemVariable => Holds value of each list item during every iteration
list => List to traverse
expressions => Statements to be executed in every iteration
Example:
Lisp
(defvar nums `(9 5 2 3))(dolist (item nums)  (print item))(write-line "")
Output:
References:
Official docs for loops in LISP
LISP-Basics
Picked
LISP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Cond Construct in LISP
Data Types in LISP
LISP - Comparison Operators on Characters & Strings
Mapping Functions in LISP
Symbols in LISP
Case Construct in LISP
Do Construct in LISP
Property Lists in LISP
Naming Conventions in LISP
Loop Construct in LISP