>> From: "Alfred M. Szmidt" <ams@gnu.org>
>> Cc: arthur.miller@live.com, emacs-devel@gnu.org
>> Date: Sat, 09 Nov 2024 11:33:45 -0500
>>
>> (while-let ((run (some-condition)))
>> (message "running"))
>>
>> Do you expect that to evaluate (some-condition) once, then, if it’s
>> initially true, run forever?
>>
>> That is how it is described in the manual, so yes (some-condition)
>> should only be done once, and not every iteration. See (elisp)
>> Conditionals .
>
>Which could mean that the manual is wrong and needs to be fixed.
>The above description actually supports what Yuri was saying, not what
>Arthur and you expect.
Mnjah; if you consider this scatchy C:
{
int foo = ..;
...
for (int i=0, j=0; u < 10i++ )
{
do something with i, j
.....
do something with foo
}
i,j are not visible here
...
}
In other words, there might be variables live outisde of
the loop-scope we wish to access in the loop, and that is
what Yuri's example shows. However, i,j are not re-initiated
on each iteration, but remembers their value. The effecto of
while-let in current implementation is that i,j are re-initiated
in each iteration, not re-evaluated, if that makes it clear.
I am not sure how to illustrate in a better way. The net effect is
that lexical variables declared in while-let loop are "read-only".
They are not, but since they are re-iniated, it is pointless to
write to them.
Of course, all loop predicates should be evaled on each iteration,
but not re-iniated on each iteration. If that makes sense. Sorry,
I am not very good at writing.
When I see at my own KISS version, I see also it only initiates
variable, but does not re-evaluate function calls on each iteration;
I didn't really udnerstand it from the beginning, so this discussion
has cleared my mind a bit too.
However I am not sure exact how to fix it. But I believe a loop
where we can't update loop invariantes is a bit strange too.
Från: Alfred M. Szmidt <ams@gnu.org>
Skickat: den 9 november 2024 17:33
Till: Yuri Khan <yuri.v.khan@gmail.com>
Kopia: arthur.miller@live.com <arthur.miller@live.com>; emacs-devel@gnu.org <emacs-devel@gnu.org>
Ämne: Re: Is this a bug in while-let or do I missunderstand it?
> If it wasn't clear, the unintuitive part is that while-let was to
> establish the local environment, so that we don't need to type:
>
> (let ((som-var (init-form)))
> (while some-var
> ... ))
But if it did it that way, the condition (init-form) would only be
evaluated once, and I’d find *that* counterintuitive. Consider the
usual form of a while loop:
(while-let ((run (some-condition)))
(message "running"))
Do you expect that to evaluate (some-condition) once, then, if it’s
initially true, run forever?
That is how it is described in the manual, so yes (some-condition)
should only be done once, and not every iteration. See (elisp)
Conditionals .
It can be convenient to bind variables in conjunction with using a
conditional. It's often the case that you compute a value, and then
want to do something with that value if it's non-‘nil’. The
straightforward way to do that is to just write, for instance:
(let ((result1 (do-computation)))
(when result1
(let ((result2 (do-more result1)))
(when result2
(do-something result2)))))
Since this is a very common pattern, Emacs provides a number of
macros to make this easier and more readable. The above can be written
the following way instead:
... following the various with various FOO-let forms, ending with
while-let.