Iteration Constructs

;; Scheme: recursion and named let are the core; do is available
(let loop ((i 0))
  (when (< i 5)
    (display i)
    (loop (+ i 1))))

(do ((i 0 (+ i 1)))                     ; do loop: (var init step)
    ((= i 5))                           ; termination test
  (display i))

(for-each (lambda (x) (display x)) '(a b c))

;; Racket's for family
(for ([i (in-range 5)]) (display i))
(for/list ([i (in-range 5)]) (* i i))   ; '(0 1 4 9 16)
(for/sum ([i (in-range 101)]) i)        ; 5050
(for*/list ([i 3] [j 2]) (list i j))    ; nested
;; Common Lisp
(dotimes (i 5) (print i))
(dolist (x '(a b c)) (print x))
(loop for i from 1 to 10 when (evenp i) collect (* i i))   ; (4 16 36 64 100)
;; Clojure: sequences and recursion with recur
(doseq [x [1 2 3]] (println x))
(for [x (range 5) :when (odd? x)] (* x x))   ; (1 9): list comprehension
(loop [i 0 acc 0]
  (if (> i 100) acc (recur (inc i) (+ acc i))))   ; 5050: recur = explicit tail call
(dotimes [i 3] (println i))