help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Closures - do you understand them well?


From: Emanuel Berg
Subject: Re: Closures - do you understand them well?
Date: Fri, 09 Dec 2022 22:31:21 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

Michael Heerdegen wrote:

>> (let ((i 0)
>>       (m (lambda (i)
>>            (lambda () i)))
>>       funs)
>>   (while (< i 100)
>>     (push (funcall m i) funs)
>>     (cl-incf i))
>>   (mapcar #'funcall funs))
>
> Yes, I think one can do this (the argument variable
> i shadows the variable of the same in the `let', but it also
> exists to conserve the value from the outside i so one can
> argue that it's ok to use the same name - it's semantically
> not incorrect anyway). Nice solution.

Okay so now it works for me as well, maybe some encoding in
messed up evaluation ...

>> (let ((i 0)
>>       funs)
>>   (while (< i 100)
>>     (let ((j i))
>>       (push (lambda () j) funs))
>>     (cl-incf i))
>>   (mapcar #'funcall funs))
>
> This is the trick that is used in the implementation of
> `dolist': create a binding that lives only inside the
> evaluation of the body of the loop in one iteration step.

Same, works.

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

[Prev in Thread] Current Thread [Next in Thread]