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: Michael Heerdegen
Subject: Re: Closures - do you understand them well?
Date: Mon, 12 Dec 2022 02:18:04 +0100
User-agent: Gnus/5.13 (Gnus v5.13)

<tomas@tuxteam.de> writes:

> > > (let ((i 0)
> > >       funs)
> > >   (while (< i 100)
> > >     (push `(lambda () ,i) funs)
> > >     (cl-incf i))
> > >   (mapcar #'funcall funs))
> >
> > Yes, but here there is no "new binding" exactly, rather the
> > value is hard-coded onto the lambda ...
>
> Look closely. There is one. Just at macro expansion (aka compile)
> time.

?!?

The expansion of (push `(lambda () ,i) funs) is

#+begin_src emacs-lisp
(setq funs
      (cons
       (list 'lambda nil i)
       funs))
#+end_src

No binding is created here, neither at compile time (where the symbol i
is not touched), nor at run-time (where the value of `i` is just passed
as an argument to `list').

Stefan would say that this accumulates a list of function expressions in
FUNS, not a list of functions, strictly speaking.  Works only because
the Lisp interpreter is made to be nice to everyone.

Michael.



reply via email to

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