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.
Create a new source code file named main.lisp and type the following code in it −
(do ((x 0 (+ 2 x)) (y 20 ( - y 2))) ((= x y)(- x y)) (format t "~% x = ~d y = ~d" x y) )
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