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

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

Mutation - do you understand it really? (was: Closures - do you understa


From: Stefan Monnier
Subject: Mutation - do you understand it really? (was: Closures - do you understand them well?)
Date: Sat, 10 Dec 2022 11:28:43 -0500
User-agent: Gnus/5.13 (Gnus v5.13)

Tassilo Horn [2022-12-10 10:34:24] wrote:
> Michael Heerdegen <michael_heerdegen@web.de> writes:
>>>  Write an expression that returns a list of 100 functions accepting
>>>  zero arguments.  `funcall'ing the Nth function must return N.  Use a
>>>  `while' loop or whatever you like but please not `dolist' or any
>>>  tool that already handles this "problem" specifically.
>>
>> Would this be a valid solution?
>>
>> #+begin_src emacs-lisp
>> (let ((i 0) (funs))
>>   (while (<= (cl-incf i) 100)
>>     (push (apply-partially #'identity i) funs))
>>   (nreverse funs))
>> #+end_src
>
> Apparently it works although I don't see why.

BTW, for those people here who find this all confusing.
I'll point out that the culprit is not "closures" but `setq` (here
hidden within `cl-incf`).

Immutable variables are just a lot better behaved (and easier to work
with for the compiler, which is why GCC/LLVM/... use an SSA conversion
to replace those nasty mutable variables with immutable ones).


        Stefan




reply via email to

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