Lisp - do construct



The do construct is also used for performing iteration using LISP. It provides a structured form of iteration.

The syntax for do statement −

(do ((variable1    value1   updated-value1)
      (variable2   value2   updated-value2)
      (variable3   value3   updated-value3)
   ...)
   (test return-value)
   (s-expressions)
)

The initial values of each variable is evaluated and bound to the respective variable. The updated value in each clause corresponds to an optional update statement that specifies how the values of the variables will be updated with each iteration.

After each iteration, the test is evaluated, and if it returns a non-nil or true, the return-value is evaluated and returned.

The last s-expression(s) is optional. If present, they are executed after every iteration, until the test value returns true.

Example - Increament/Decrement Numbers till Equality

In this example, we're incrementing and decrementing variables in do construct and printing the variables. Create a new source code file named main.lisp and type the following code in it −

main.lisp

; perform a do loop
(do ((x 0 (+ 2 x)) ; initialize x to 0 and increment by 2
   (y 20 ( - y 2))) ; initialize y to 20 and decrement by 2
   ((= x y))  ; checking if x and y is same
   ; print values
   (format t "~% x = ~d  y = ~d" x y)
)

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

x = 0  y = 20
x = 2  y = 18
x = 4  y = 16
x = 6  y = 14
x = 8  y = 12

Example - Increament/Decrement Numbers till Increasing one is greater

In this example, we're incrementing and decrementing variables in do construct and printing the variables. Create a new source code file named main.lisp and type the following code in it −

main.lisp

; perform a do loop
(do ((x 0 (+ 2 x)) ; initialize x to 0 and increment by 2
   (y 20 ( - y 2))) ; initialize y to 20 and decrement by 2
   ((> x y))  ; checking if x is greater than y
   ; print values
   (format t "~% x = ~d  y = ~d" x y)
)

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

x = 0  y = 20
x = 2  y = 18
x = 4  y = 16
x = 6  y = 14
x = 8  y = 12
x = 10 y = 10

Example - Increament/Decrement Numbers till Decreasing one is lesser

In this example, we're incrementing and decrementing variables in do construct and printing the variables. Create a new source code file named main.lisp and type the following code in it −

main.lisp

; perform a do loop
(do ((x 0 (+ 2 x)) ; initialize x to 0 and increment by 2
   (y 20 ( - y 2))) ; initialize y to 20 and decrement by 2
   ((< y x))  ; checking if y is less than y
   ; print values
   (format t "~% x = ~d  y = ~d" x y)
)

Output

When you click the Execute button, or type Ctrl+E, LISP executes it immediately and the result returned is −

x = 0  y = 20
x = 2  y = 18
x = 4  y = 16
x = 6  y = 14
x = 8  y = 12
x = 10 y = 10
lisp_loops.htm
Advertisements