Go to the first, previous, next, last section, table of contents.

Iteration Constructs

You may have noticed that we haven't discussed iteration constructs much. Scheme does include iteration constructs like do, which we'll describe later, but you'll use them less than in most other languages. It's usually easier to use recursion, once you get the hang of it. When you do use iteration constructs, you should also understand that they're really syntactic sugar for recursion.

For most purposes, you can use Scheme's iteration constructs as you would in other languages, but they're actually interestingly different. Scheme's iteration constructs are really syntactic sugar for tail recursion. Anything you can do iteratively, you can do with recursion, and recursion lets you do other things that normal iteration doesn't.

The main difference between Scheme's iteration constructs and the ones you may be used to is that loop variables aren't updated at each iteration. This doesn't mean you don't have loop variables--the difference is that loop variables are rebound at each iteration (tail call), rather than being bound once on entry to the loop, and updated (assigned to) at each iteration.

(Don't worry if this doesn't make sense yet--it will later.)

It turns out that having a new binding of the loop variable at each iteration is very convenient when using first-class procedures and continuations. For example, if you create a first-class procedure in a loop body, it can continue to refer to the variable binding for the iteration of the loop that created it.


Go to the first, previous, next, last section, table of contents.