2.7 Unconditional execution

2.7.1 Reference pages

do Unconditional Execution Clause Construct

doing Unconditional Execution Clause Construct

Syntax:do[ing] [expr]*

Thedo construct simply evaluates the specified expressions wherever they occur in the expanded form ofloop.

The expr argument can be any nonatomic Common Lisp form. Each expr is evaluated in every iteration.

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.

Because every loop clause must begin with a loop keyword, you would use the keyworddo when no control action other than execution is required.

;; Print some numbers.
> (loop for i from 1 to 3
        do (print i))
1
2
3
NIL

;; Print numbers and their squares. ;; The DO construct applies to multiple forms. > (loop for i from 1 to 3 do (print i) (print (* i i))) 1 1 2 4 3 9 NIL

return Unconditional Execution Clause Construct

Syntax:return expr

Thereturn construct terminates a loop and returns the value of the specified expression as the value of the loop. This construct is similar to the Common Lisp special formreturn-from and the Common Lisp macroreturn.

The Loop Facility supports thereturn construct for backwards compatibility with older loop implementations.

Thereturn construct returns immediately and does not execute anyfinally clause that is given.

;; Signal an exceptional condition.
> (loop for item in '(1 2 3 a 4 5)
        when (not (numberp item))
        return (cerror "enter new value" 
                       "non-numeric value: ~s" item))

>>Error: non-numeric value: A EVAL: Required arg 0 (EXPRESSION): (LOOP FOR ITEM IN (QUOTE (1 2 3 A 4 5)) WHEN (NOT (NUMBERP ITEM)) RETURN (CERROR "enter new value" "non-numeric value: ~s" ITEM)) :C 0: enter new value :A 1: Abort to Lisp Top Level -> :a Abort to Lisp Top Level Back to Lisp Top Level

;; The previous example is equivalent to the following one. > (loop for item in '(1 2 3 a 4 5) when (not (numberp item)) do (return (cerror "enter new value" "non-numeric value: ~s" item)))

>>Error: non-numeric value: A EVAL: Required arg 0 (EXPRESSION): (LOOP FOR ITEM IN (QUOTE (1 2 3 A 4 5)) WHEN (NOT (NUMBERP ITEM)) DO (RETURN (CERROR "enter new value" "non-numeric value: ~s" ITEM))) :C 0: enter new value :A 1: Abort to Lisp Top Level -> :a Abort to Lisp Top Level Back to Lisp Top Level


The Loop Facility - 9 SEP 1996

Generated with Harlequin WebMaker