Common Lisp the Language, 2nd Edition

Next: Order of Execution Up: Loop Previous: How the Loop

# 26.3. Parsing Loop Clauses

The syntactic parts of a loop construct are called clauses; the scope of each clause is determined by the top-level parsing of that clause's keyword. The following example shows a loop construct with six clauses:

    (loop for i from 1 to (compute-top-value)         ;First clause
          while (not (unacceptable i))                ;Second clause
          collect (square i)                          ;Third clause
          do (format t "Working on ~D now" i)         ;Fourth clause
          when (evenp i)                              ;Fifth clause
            do (format t "~D is a non-odd number" i)
          finally (format t "About to exit!"))        ;Sixth clause

Each loop keyword introduces either a compound loop clause or a simple loop clause that can consist of a loop keyword followed by a single Lisp form. The number of forms in a clause is determined by the loop keyword that begins the clause and by the auxiliary keywords in the clause. The keywords `do`, `initially`, and `finally` are the only loop keywords that can take any number of Lisp forms and group them as if in a single `progn` form.

Loop clauses can contain auxiliary keywords, which are sometimes called prepositions. For example, the first clause in the preceding code includes the prepositions `from` and `to`, which mark the value from which stepping begins and the value at which stepping ends.

- Order of Execution
- Kinds of Loop Clauses
- Loop Syntax

Next: Order of Execution Up: Loop Previous: How the Loop