Iteration

Implementation extras: Racket's for family, Guile's while, SRFI-42 eager comprehensions ((list-ec (: i 5) (* i i))), and Alex Shinn's (chibi loop) / foof-loop.

;; do: (do ((var init step) ...) (test result ...) body ...)
(do ((i 0 (+ i 1)))
    ((= i 5))
  (display i))                          ; 01234

(do ((vec (make-vector 5))
     (i 0 (+ i 1)))
    ((= i 5) vec)
  (vector-set! vec i (* i i)))          ; #(0 1 4 9 16)

;; named let: the idiomatic loop
(let loop ((i 1))
  (when (<= i 3)
    (display i)
    (newline)
    (loop (+ i 1))))

;; Loop with an early exit using call/cc (Module 25) or simply returning
(define (index-of x lst)
  (let loop ((l lst) (i 0))
    (cond ((null? l) #f)
          ((equal? (car l) x) i)
          (else (loop (cdr l) (+ i 1))))))
(index-of 'c '(a b c))                  ; 2

;; A while macro (defined with syntax-rules, Module 31)
(define-syntax while
  (syntax-rules ()
    ((_ cond body ...)
     (let lp () (when cond body ... (lp))))))
(define n 0)
(while (< n 3) (display n) (set! n (+ n 1)))   ; 012