Why is the output of this Lisp do loop different than expected?
Body:
I have the following Common Lisp code:
(setq x 10)
(setq y 30)
(do ((x 0 (+ 3 x))
(y 20 (- y 2))
(z (- y x) (- z x)))
((> x y) (- x y)) ; Exit condition
(format t "~% z = ~d" z))
(format t "x = ~d y = ~d" x y)
I expect the output to be:
z = 20
z = 17
z = 11
z = 2
z = -10
x = 15 y = 10
However, when I run this code, the compiler tells me this is not the correct solution. I’m confused about the following:
How are the variables x, y, and z being updated during each iteration?
Why is my expected output incorrect?
What is the correct output and why?
Could someone help me understand the flow of execution for this code and the actual output?
Hasan Saafen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1