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: Tassilo Horn
Subject: Re: Closures - do you understand them well?
Date: Sat, 10 Dec 2022 10:34:24 +0100
User-agent: mu4e 1.9.4; emacs 30.0.50

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.  Where's the difference to
this inlined version which returns (101 ... 101)?

--8<---------------cut here---------------start------------->8---
(let ((i 0) (funs))
  (while (<= (cl-incf i) 100)
    (push (lambda (&rest args2)
            (apply #'identity (append (list i) args2)))
          funs))
  (mapcar #'funcall (nreverse funs)))
--8<---------------cut here---------------end--------------->8---

Bye,
Tassilo



reply via email to

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