Subject: Re: modifying a loop variable legal?
From: (Rob Warnock)
Date: Tue, 14 Nov 2006 06:12:40 -0600
Newsgroups: comp.lang.lisp
Message-ID: <> <> wrote:
| JP Massar wrote:
| > Is this defined common lisp or is the result undefined?
| > (LOOP FOR x FROM 1 TO 10
| >        DO (SETF x (+ x 2))
| >       COLLECT x)
| > Perusal of the hyperspec suggests that is in fact valid,
| > but I had always assumed it was not.
| Yes, I do it all the time! (Although maybe I shouldn't?)

I personally consider it bad style, since it tends to hide from
the casual reader the actual sequence of values X takes. I usually
find that some other variation is more perspicuous, e.g., the
following yields the same result as the above:

    (loop for x from 3 to 12 by 3
      collect x)

Though I confess I *have* occasionally indulged in somewhat complex
BY clauses in LOOP, e.g., an example I posted a few weeks ago:  ;-}

    (flet ((stepper (list)
             (case (car list)
               ((:one)   (cddr list))
               ((:two)   (cdddr list))
               ((:three) (cdddr list))
               (otherwise (cdr list)))))
      (loop for (key a b c) on list by #'stepper

    which can parse lists like this:

    (:one 123 :two 234 453 :three 7 5 8 :special :other 99 22 :two 34 54)


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607