by brabel · 1y ago · 0 comments
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.---
(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 }