Liste des Groupes | Revenir à cl c |
On 2025-04-15, bart <bc@freeuk.com> wrote:That's sort of the point. It is not 'for' as is it generally understood. It's more of a souped-up while.On 15/04/2025 08:17, Janis Papanagnou wrote:That's mostly bike shedding. It could also have been called:and someone already posted another iteration type on linked lists>
>
for (node = list; node; node = node->next)
These two I get, and I can tell you that they're all WHILE loops. To
make make them tidier, I'd have fewer complaints about them if they
instead looked like this:
>
while (node = list; node; node = node->next)
do (node = list; node; node = node->next)
loop (node = list; node; node = node->next)
Lisp called this kind of general variable stepping loop "do"In what I recognise as 'for' loops, any variable is specified only once in the header. Any initialisation and increment is handled automatically.
(do ((var1 init1 step1)
(var2 init1 step2)
...)
(until-this-is-true also-this ... yield-this-result)
do-this
and-this
...)
Naming things can be hard. The reason that the loop is called for
is that most instance of for have a focus on the specific variable
that is initialized, and the guard condition has to do with it
hitting some specific terminating value.
I've looked in the past at CLisp and its loops. My view is that the number of possibilities it provides is over the job. It's like somebody was tasked with thinking up as many as possible, and making them all available.So, you can optionally add extra elements to a while (x).Common Lisp uses "for" to indicate several kinds of variable
>
It would be necessary to either define whether while(x; y) means while
(; x; y) or while (x; y), or to always require either zero or two
semicolons inside (...).
>
That could have kept 'for' for how it works in other languages (the ones
that haven't just blindly copied C's version!).
stepping clauses inside the loop syntax, some of which are:
[4]> (loop for x in '(a b c)
for y = 1 then (1+ y)
for z = (* y 10)
with w = (* y 10)
collect (list x y z w))
((A 1 10 10) (B 2 20 10) (C 3 30 10))
When loop is used to simulate the classic Lisp do loop,That's a poor way to do it. I suggested elsewhere using only inclusive ranges, and using '-1' as a postfix 'operator' to treat it as exclusive.
it's done with the "for var = init then step" clauses.
There is a "for var from N to M" clause, as well as "for var below N"
which goes from 0 to N-1.
But this:Since it can always be trivially be written as:for (;cond;)That is simply untidy looking.
Les messages affichés proviennent d'usenet.