Next: , Previous: Collection Macros, Up: Reference


3.5 for*

— Macro: for* bindings &body body ⇒ body-value*

for* steps variables with support for a small but useful subset of loop's for clause, which you can combine with any looping construct whatsoever. Simply wrap the looping construct with for* and then call the local function step* at the start of each loop body iteration. The first time step* is called, the variables are initialized to their first value. On subsequent calls, the values are updated. The updates are always made in sequence, analogously to let* and setf. It is not semantically meaningful to read the variables before step* has been called at least once.

for* deals with stepping of variables only. It never initiates a termination of looping. This is to avoid interfering with the semantics of the looping construct we're wrapping. If you find yourself wishing for such a feature, then simply make the termination test right at the start of the looping construct's body (right after the step* call) and use any appropriate non-local exit construct.

Each binding in bindings is of the form (var . stepping). Here are the supported stepping clauses:

for-as-arithmetic
You can use from, upfrom, downfrom and by like you would with loop: from start or upfrom start for incrementing from start, downfrom start for decrementing. Use by step, a non-negative integer, to control the stepping (default is 1).

Here's an example of a for binding clause to create a local variable index that will successively take the values ‘10, 7, 4, 1, -2’, etc.:

               (index downfrom 10 by 3)

for-as-equals
You can use = and then like you would with loop. There are two possible forms of this statement:
               (var = form)

In this case, a binding var is created, and will be set to the value form evaluates to on each iteration. Note that it is stylistically inappropriate to use this form of stepping if there is no further for* binding which needs to refer to the value of this variable. Use a more conventional binding construct such as let inside the body of the loop instead.

               (var = first-form then then-form)

In this case, a binding var is created, and will be set to the value first-form evaluates to on the first iteration (the first time step* is called), and then to the value then-form evaluates to on subsequent iterations.

— Local Function: step* ⇒ nil

step* is used inside for* to step the variables.
Its semantics are described in the definition of for* above.