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

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

Re: How to avoid compiler warning `unused lexical variable' for `dolist'


From: Leo Butler
Subject: Re: How to avoid compiler warning `unused lexical variable' for `dolist' or `dotimes'?
Date: Mon, 11 Jan 2021 06:53:13 -0600
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> ********************************************************
> Caution: This message was sent from outside the University of Manitoba.
> ********************************************************
>
>>> This distinction becomes relevant if BODY modifies VAR or captures it
>>> in a closure.
>> Maybe I am being thick, but I cannot think of how there would be a
>> different behaviour.
>
> Try:
>
>     (let (funs)
>       (dotimes (i 5)
>         (push (lambda () i) funs))
>       (mapcar #'funcall funs))
>
> IIUC the version you advocate would return
>
>     (5 5 5 5 5)
>
> whereas the current version returns
>
>     (4 3 2 1 0)

Thanks, I was being thick.

----

I realize that elisp does not claim to be a dialect of common lisp, but
it would be helpful to note that this behaviour deviates from the
behaviour of several CL implementations (gcl, clisp, sbcl, ecl).

The CL standard (http://clhs.lisp.se/Body/m_dotime.htm) says:

 ....
 It is implementation-dependent whether dotimes establishes a new
 binding of var on each iteration or whether it establishes a binding
 for var once at the beginning and then assigns it on any subsequent
 iterations. 



>
> Other differences occur if you do
>
>     (let (vals)
>       (dotimes (i 10)
>         (push (setq i (1+ i)) vals))
>       vals)

Yes.

Leo



reply via email to

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