4 ms·
If you've never done Common Lisp before, it's better to start by looking at `loop`, which is a built-in macro that provides a bunch of different ways to loop/it
by stevelosh 10y ago
If you've never done Common Lisp before, it's better to start by looking at `loop`, which is a built-in macro that provides a bunch of different ways to loop/iterate: http://www.gigamonkeys.com/book/loop-for-black-belts.html http://www.gigamonkeys.com/book/loop-for-black-belts.html
iterate is a Common Lisp library that aims to be a replacement for loop, being cleaner (subjectively) and portably extensible: https://common-lisp.net/project/iterate/ https://common-lisp.net/project/iterate/
`repeat` is an iterate clause that tells iterate to perform N iterations of the loop:
(iterate (repeat 3) (print 'beep))
BEEP
BEEP
BEEP
As for this particular `for`, it's an iterate clause that handles numeric iteration (among other things). So:
(iterate (for x :from 0)
(for y :from 10 :downto 7)
(print (list x y)))
Would give you:
(0 10)
(1 9)
(2 8)
(3 7)
iterate will terminate the loop the first time any one of its clauses runs out of values/iterations. So in the post I had something like
(iterate (repeat N)
(for x :from start)
...)
You could write this as
(iterate (for x :from start :below (+ start N))
...)
But manually calculating the ending value seems ugly to me. I know I want the loop to happen N times so I just say `(repeat N)` and let the computer figure out when it's time to be done.
- SixSigma 10y agoI cant remember the original or who I'm almost quoting but it goes something like this : > `loop` is so complex, LISP programmers invented functional style so they didn't have to learn how to use it