Next: Examples of Miscellaneous Loop Features, Previous: Conditional Execution Clauses, Up: The LOOP Facility
The named construct
establishes a name for an implicit block surrounding the
entire
loop so that the return-from special operator can be used to return
values from or to exit loop.
Only one name per loop form can be assigned.
If used, the named construct must be the first clause in the loop expression.
The return construct takes one form.
Any values returned by the form
are immediately returned by the loop form.
This construct is similar to the return-from special operator and the return macro.
The return construct
does not execute any finally clause that
the loop form
is given.
;; Just name and return.
(loop named max
for i from 1 to 10
do (print i)
do (return-from max 'done))
▷ 1
→ DONE
The initially and finally constructs
evaluate forms that occur before and after the loop body.
The initially construct causes the supplied
compound-forms
to be evaluated
in the loop prologue, which precedes all loop code except for
initial settings supplied by constructs with, for, or
as.
The code for any initially clauses is
executed
in the order in which the clauses appeared in
the loop.
The finally construct causes the supplied
compound-forms
to be evaluated
in the loop epilogue after normal iteration terminates.
The code for any finally clauses is
executed
in the order in which the clauses appeared in
the loop. The collected code is executed once in the loop epilogue
before any implicit values are returned from the accumulation clauses.
An explicit transfer of control (e.g., by return, go, or throw)
from the loop body, however, will exit the
loop without executing the epilogue code.
Clauses such as return, always, never, and
thereis
can bypass the finally clause.
return (or return-from, if the named option was supplied)
can be used after finally to return values from a loop.
Such an explicit return
inside the
finally clause takes precedence over returning the accumulation
from clauses supplied by such keywords as collect, nconc,
append, sum, count, maximize, and
minimize;
the accumulation values for these preempted clauses are not returned by
loop if return or return-from is used.