
2.8 Miscellaneous features
initially [expr]*
finally [expr]*
finally unconditional-clause
initially construct causes the specified expression to be evaluated in the loop prologue, which precedes all loop code except for initial settings specified by constructswith,for, oras.
finally construct causes the specified expression to be evaluated in the loop epilogue after normal iteration terminates.
return,always,never, andthereis can bypass thefinally clause.
return afterfinally to return values from a loop. The evaluation of thereturn form inside thefinally clause takes precedence over returning the accumulation from clauses specified by such keywords ascollect,nconc,append,sum,count,maximize, andminimize; the accumulation values for these preempted clauses are not returned by the loop ifreturn is used.
do,initially, andfinally are the only loop keywords that take an arbitrary number of forms and group them as if by using an implicitprogn.
;; This example parses a simple printed string representation
;; from BUFFER (which is itself a string) and returns the index
;; of the closing double-quote character.
> (loop initially (unless (char= (char buffer 0) #\")
(loop-finish))
for i fixnum from 1 below (length buffer)
when (char= (char buffer i) #\")
return i)
;; The FINALLY clause prints the last value of I.
;; The collected value is returned.
> (loop for i from 1 to 10
when (> i 5)
collect i
finally (print i))
11
(6 7 8 9 10)
;; Return both the count of collected numbers and the numbers.
> (loop for i from 1 to 10
when (> i 5)
collect i into number-list
and count i into number-count
finally (return (values number-count number-list)))
5
(6 7 8 9 10)
named Loop Construct
named name
named construct allows you to assign a name to a loop construct so that you can use the Common Lisp special formreturn-from to exit the named loop.
named construct must be the first clause in the loop expression.
;; Just name and return.
> (loop named max
for i from 1 to 10
do (print i)
do (return-from max 'done))
1
DONE

Generated with Harlequin WebMaker