The return
special form mentioned in the section on iteration is an
example of a nonlocal return. Another example is the return
-from form,
which returns a value from the surrounding function:
> (defun foo (x)
(return-from foo 3)
x)
FOO
> (foo 17)
3
Actually, the return-from
form can return from any named block -- it's
just that functions are the only blocks which are named by default. You
can create a named block with the block special form:
> (block foo
(return-from foo 7)
3)
7
The return
special form can return from any block named
nil
. Loops are
by default labelled nil
, but you can make your own nil-labelled blocks:
> (block nil
(return 7)
3)
7
Another form which causes a nonlocal exit is the error
form:
> (error "This is an error")
Error: This is an error
The error
form applies format to its arguments, then places you in the
debugger.