# recur

`(recur NAME ((NAME EXPRESSION) ...) EXPRESSION)`

A 'recur' expression is a short-hand looping construct. In consists of three parts. The first is a name; the second is a sequence of names and expressions and the last is an expression. The first names the loop, the second is the variables that change for each iteration of the loop and the last expression is the body of the loop.

The evaluation of a recur expression first evaluates all of the expressions in the sequence of names and expressions. Then, the names in the second part of the recur are bound to the results of those expressions, and the first name is bound to a procedure that accepts one argument for each name in the second part of the recur expression. Invoking that procedure will iterate the loop. Finally, the body is evaluated in the context of those bindings.

For example, this is an accumulator-style version of length:

    (define (length l)
      (let loop ((lst l) (len 0))
        (cond
         ((empty? lst) len)
         (else
          (loop
           (rest lst)
           (+ len 1))))))

When length is called, it begins the loop. The first iteration, `lst` is bound to the argument of length and `len` is bound to zero. Then, if `lst` is not empty, the loop repeats, with `len` bound to a number one larger than the last iteration and `lst` bound to the rest of the list. Finally, when `lst` is empty, the accumulated length is returned.

Advanced Student Table of Contents