Skip to content

Commit 44e35ae

Browse files
committed
iteration: for accepts an else. Trick to alternate a boolean.
1 parent 2cb80e3 commit 44e35ae

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

iteration.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,29 @@ so it turns out we can specify the type before the `=` and chain the `with` with
560560
collect (list x foo bar))
561561
~~~
562562

563+
We can also give `for` an `else` clause that will be called at each iteration:
564+
565+
~~~lisp
566+
(loop for x in '(1 2 3)
567+
for intermediate = 10 then (incf intermediate)
568+
do (print intermediate))
569+
10
570+
11
571+
12
572+
~~~
573+
574+
Here's a trick to alternate a boolean:
575+
576+
~~~lisp
577+
(loop for x in '(1 2 3 4)
578+
for up = t then (not up)
579+
do (print up))
580+
581+
T
582+
NIL
583+
T
584+
NIL
585+
~~~
563586

564587
### Loop with a counter
565588
#### loop

0 commit comments

Comments
 (0)