closures and dynamic binding
greg
greg at cosc.canterbury.ac.nz
Sat Oct 4 21:47:44 EDT 2008
Hrvoje Niksic wrote:
> Common Lisp behaves similar to Python in this
> regard:
>
> * (loop for i from 0 to 2 collect (lambda () i))
I wouldn't call the CL loop macro the equivalent of
a for-loop. It's more like a while-loop.
The Lisp equivalent of a Python for-loop would be
to use one of the mapping functions. Then, since
the loop body is a lambda, you get a new scope for
each iteration automatically.
> I don't think it has anything to do with variable leaking out of the
> loop.
That's right, it doesn't. If I were designing a
for-loop from scratch, I wouldn't let it leak, but
that part seems to be necessary for backwards
compatibility.
> In contrast, Scheme regards
> iteration as a special case of recursion, and R5RS "do" prescribes
> assigning loop variables to "fresh locations" to match what recursion
> normally does.
I'd say it prescribes that because it's useful
behaviour, not because it has anything to do with
recursion.
--
Greg
More information about the Python-list
mailing list