Iteration Control

Most of the so-called iteration control clauses start with the loop keyword for, or its synonym as,2 followed by the name of a variable. What follows after the variable name depends on the type of for clause.

The subclauses of a for clause can iterate over the following:

  • Ranges of numbers, up or down, by specified intervals
  • The individual items of a list
  • The cons cells that make up a list
  • The elements of a vector, including subtypes such as strings and bit vectors
  • The pairs of a hash table
  • The symbols in a package
  • The results of repeatedly evaluating a given form

A single loop can have multiple for clauses with each clause naming its own variable. When a loop has multiple for clauses, the loop terminates as soon as any for clause reaches its end condition. For instance, the following loop:

  1. (loop
  2. for item in list
  3. for i from 1 to 10
  4. do (something))

will iterate at most ten times but may stop sooner if list contains fewer than ten items.