Lisp DO variable syntax reasoning
Asked Answered
T

1

6

In Peter Seibel's Practical Common Lisp, he gives this example:

(do ((nums nil) (i 1 (1+ i)))
    ((> i 10) (nreverse nums))
  (push i nums))

I can see how it works, using nums inside the loop but not giving it a step-form. Why would you put nums in the variable-definition rather than do this:

(let (nums) (do ((i 1 (+ i 1)))
         ((> i 10) (nreverse nums))
       (push i nums)))

I'm sure there's a good reason, but I don't get it yet.

Terramycin answered 29/10, 2008 at 19:52 Comment(0)
M
12

Because it's convenient and saves indentation. Furthermore, the accumulator conceptually belongs to the loop, so why not put it there?

Moazami answered 29/10, 2008 at 20:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.