Skip to content
TurtleKitty edited this page May 11, 2019 · 4 revisions

for

This operator is used for looping in imperative programs.
Syntax: (for init-list predicate-expr update-expr body-expr body-expr ...)

(for (i (cell 0) total (cell 0)) (<= i.get 20) (i.set! i.get.inc)
    (total.set! (+ total.get i.get))
    total.get)

; 210

next will skip to the next iteration of the loop.

(for (i (cell 0) total (cell 0)) (<= i.get 20) (i.set! i.get.inc)
    (when (mod i.get 2)
        (next total.get))
    (total.set! (+ total.get i.get))
    total.get)

; 110

redo restarts a loop iteration without running the update expression.

(for (i (cell 0) total (cell 0)) (<= i.get 20) (i.set! i.get.inc)
    (when (mod total.get 2)
        (total.set! total.get.inc)
        (redo total.get))
    (total.set! (+ total.get i.get))
    total.get)

; 220

last terminates a loop.

(for (i (cell 0) total (cell 0)) (<= i.get 20) (i.set! i.get.inc)
    (when (> total.get 100)
        (last total.get))
    (total.set! (+ total.get i.get))
    total.get)

; 105
Clone this wiki locally