Next: , Up: Other Features

3.2 Named Blocks

Like Common Lisp blocks, iterate forms can be given names. The name should be a single symbol, and it must be the first form in the iterate. The generated code behaves exactly like a named block; in particular, (return-from name) can be used to exit it:

  (iter fred
        (for i from 1 to 10)
        (iter barney
              (for j from i to 10)
              (if (> (* i j) 17)
                  (return-from fred j))))

An iterate form that is not given a name is implicitly named nil.

Sometimes one would like to write an expression in an inner iterate form, but have it processed by an outer iterate form. This is possible with the in clause.

— Clause: in name &rest forms

Evaluates forms as if they were part of the iterate form named name. In other words, iterate clauses are processed by the iterate form named name, and not by any iterate forms that occur inside name.

As an example, consider the problem of collecting a list of the elements in a two-dimensional array. The naive solution,

       (iter (for i below (array-dimension ar 0))
             (iter (for j below (array-dimension ar 1))
                   (collect (aref ar i j))))

is wrong because the list created by the inner iterate is simply ignored by the outer one. But using in we can write:

       (iter outer (for i below (array-dimension ar 0))
             (iter (for j below (array-dimension ar 1))
                   (in outer (collect (aref ar i j)))))

which has the desired result.