Lp-section-5
Informal Semantics of FOR Loop$s
LP5: Informal Semantics of FOR Loop$s
The value returned by a FOR loop$ can be specified as follows
— but we hasten to add that this is not how Common Lisp compilers
implement loop$! We think this description is easier to understand.
The first step in evaluating a FOR loop$ is to determine the
range of each iteration variable by evaluating the target expressions as
previously described.
Next, repeat the following steps until the process stops.
(1) If any range is empty, stop. (2) Otherwise, assign each iteration
variable the first value in its range and shorten the range by one, by
removing that first element. (3) If the until-expr evaluates to
non-nil under the current values of the global and iteration variables,
stop. Otherwise, if the when-expr evaluates to non-nil under the
current values of the variables, then evaluate the body-expr under the
current values. Otherwise, do not evaluate the body-expr.
When the repetition stops, the body-expr will have been evaluated
n times, each time producing a value vali, 0 <= i <
n, where the vali are listed in the order in which they were
produced. The result returned by the loop$ is determined by the
operator, op, of the loop$ as follows.
- SUM — return the sum of the vali
- COLLECT — return (LIST val1 ... valn)
- APPEND — return (APPEND val1 ... valn)
- ALWAYS — return (AND val1 ... valn T)
- THEREIS — return (OR val1 ... valn)
Note that if op is ALWAYS, iteration can stop the first time a
vali is nil. Similarly, if op is THEREIS, iteration
can stop the first time a vali is non-nil. Note also that an
ALWAYS loop$ returns NIL or T but a THEREIS
loop$ returns nil or the first non-nil value produced by the
evaluation of the body.
Now go to lp-section-6 (or return to the Table of Contents).