2.8 Miscellaneous features
finally Loop Construct
initiallyconstruct causes the specified expression to be evaluated in the loop prologue, which precedes all loop code except for initial settings specified by constructs
finallyconstruct causes the specified expression to be evaluated in the loop epilogue after normal iteration terminates.
thereiscan bypass the
finallyto return values from a loop. The evaluation of the
returnform inside the
finallyclause takes precedence over returning the accumulation from clauses specified by such keywords as
minimize; the accumulation values for these preempted clauses are not returned by the loop if
finallyare the only loop keywords that take an arbitrary number of forms and group them as if by using an implicit
;; 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)named Loop Construct
;; 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)
namedconstruct allows you to assign a name to a loop construct so that you can use the Common Lisp special form
return-fromto exit the named loop.
namedconstruct 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