3 ms·
I suspect you'll love Common Lisp's LOOP: https://gigamonkeys.com/book/loop-for-black-belts https://gigamonkeys.com/book/loop-for-black-belts Example: (lo
by brabel 2y ago
I suspect you'll love Common Lisp's LOOP: https://gigamonkeys.com/book/loop-for-black-belts https://gigamonkeys.com/book/loop-for-black-belts
Example:
(loop repeat 8
;; init x, then decide how to "prepare" next iteration
for x = 0 then (+ (* 2 x) 1)
collect x)
(0 1 3 7 15 31 63 127)
You can insert a condition check in the middle, of course:
(loop repeat 8
for x = 0 then (+ (* 2 x) 1)
;; but stop if x gets too big
while (< x 100)
collect x)
(0 1 3 7 15 31 63)
And much, much more. It's the ultimate loop construct.
- gpderetta 2y agoLambda The Ultimate Loop!
- lispm 2y agoactually, according to the LOOP syntax, the REPEAT clause has to follow the FOR clause...
- sparkie 2y agoDon't forget about the `prog*` family. --- (prog1 foo bar*) Evaluates foo, then bar(s), and returns the result of evaluating foo and discards the results of bar(s). Useful if `foo` is the condition and you need to perform some change to it immediately after, eg: (while (prog1 (< next prev) (setq prev next)) ...) --- (prog2 foo bar baz*) Evaluates foo, then bar, then baz(s) (if present), returns the result of evaluating bar and discards the results of evaluating foo and baz(s). Might be what GP wants. `foo` is the preparation, `bar` is the condition`, and `baz` can be some post-condition mutation on the compared value. Not too dissimilar to for (pre, cond, post) {} With `prog2` you could achieve similar behavior with no built in `for`: (while (prog2 pre cond post) ...) --- (progn foo*) Evaluate each foo in order, return the result of evaluating the last element of foo and discard all the others. `progn` is similar to repeated uses of the comma operator in C, which GP has possibly overlooked as one solution. while (prepare, condition) { process }
- taeric 2y agoLearning to embrace the LOOP construct has been an experience, for me. Same with the FORMAT abilities. It is amazing how much hate they both get, for how capable they both are.