2.8 Miscellaneous features

2.8.3 Reference pages

initially Loop Construct

finally Loop Construct

Syntax:initially [expr]*

Syntax:finally [expr]*

Syntax:finally unconditional-clause

Theinitially 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.

Thefinally construct causes the specified expression to be evaluated in the loop epilogue after normal iteration terminates.

The expr argument can be any nonatomic Common Lisp form.

Clauses such asreturn,always,never, andthereis can bypass thefinally clause.

You can use the Common Lisp macroreturn 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.

The constructsdo,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) #\")
        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

Syntax:named name

Thenamed 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.

You can assign only one name per loop; the specified name becomes the name of the implicit block for the loop. If used, thenamed 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))

The Loop Facility - 9 SEP 1996

Generated with Harlequin WebMaker