LOOP quiz

  1. Does (loop for i below 10 finally (return i)) return 9 or 10?
  2. Does (loop for i upto 10 finally (return i)) return 10 or 11?
  3. What does (loop for i below 10 for j upto 10 finally (return (list i j))) return?
  4. What about (loop for i below 10 and j upto 10 finally (return (list i j)))?

I stumbled upon the semantics of this last example in a recent bugfix and thought it was worth sharing. (Reminded me of the joke about what's hard in CS, too.)

Turns out LOOP's FOR ... AND not only mimics LET (rather than LET*) in terms of binding visibility, it also influences when the loop termination checks take place. That was new to me. I initially expected examples 3 and 4 to return the same values. What about you? Which ones, if any, did you get wrong? :-)

P.S.: LOOP for Black Belts is my favorite LOOP tutorial.

5 komentoj:

Anonymous said...

Hah. I don't know what the right answers for the last two are, but CLISP and SBCL don't give the same result.

Curt said...

For what it's worth, I get the same results between SBCL (1.2.2) and CCL (1.10)

A. V. said...

> What about you? Which ones, if any, did you get wrong? :)

1, 2, 3, 4

Luís said...

Heh. Is this aspect of loop (#4) undefined behaviour? I couldn't find much about it in the spec.

André Miranda Moreira said...

Missed all four questions! Need to do some loop homework.