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: tomas
Subject: Re: Closures - do you understand them well?
Date: Mon, 12 Dec 2022 06:16:30 +0100

On Mon, Dec 12, 2022 at 02:18:04AM +0100, Michael Heerdegen wrote:
> <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
                                     ^^^

To me, that one (well, if you squint, the others too)
counts as a compile time binding, of sorts. Or well,
pre-compile or something.

It was the "poor human's closure" in pre-lexical times,
after all.

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

Yes, the binding of i is effective before the lambda
expression is "compiled" (whatever that means when you
are playing interpreter, but it still means something,
no?)

> 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.

FWIW, I find those languages (the ones trying to be nice)
the most interesting. But they can be confusing, too :-)

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


reply via email to

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