Next: , Previous: , Up: baselib   [Index]


4.15 Iteration

Syntax: let ?variable ?bindings ?body

“Named let” is a variant on the syntax of let that provides a general looping construct and may also be used to express recursion. It has the same syntax and semantics as ordinary let except that ?variable is bound within ?body to a procedure whose parameters are the bound variables and whose body is ?body. Thus the execution of ?body may be repeated by invoking the procedure named by ?variable.

(let loop ((numbers '(3 -2 1 6 -5))
           (nonneg '())
           (neg '()))
  (cond ((null? numbers) (list nonneg neg))
        ((>= (car numbers) 0)
         (loop (cdr numbers)
               (cons (car numbers) nonneg)
               neg))
        ((< (car numbers) 0)
         (loop (cdr numbers)
               nonneg
               (cons (car numbers) neg)))))
⇒ ((6 1 3) (-5 -2))