The general structure of the do form is as follows:
(do ((<variable1> <init1> <step1>) ...) (<test> <expression> ...) <command> ...)
Paraphrasing http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-ZH-6.html#node_chap_5 , each iteration begins with a <test> score, if it evaluates the true value, <expression> are evaluated from left to right and last value if the result of the form do . In your second example, = will be evaluated as a boolean true, then I will be evaluated, and finally 5 is the return value of the form. In the first case (= i 5) this is a test, and the do form returns undefined. A typical way to write a loop would look something like this:
(do ((i 0 (+ i 1))) ((= i 5) i) ; maybe return the last value of the iteration (display i))
You do not need an explicit mutation of the loop variable, since it is processed by the <step> expression.
source share