1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
<HTML>
<HEAD>
<TITLE>Common LISP Hints: Non-local Exits</TITLE>
</HEAD>
<BODY>
<A HREF="LISP-tutorial-18.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-20.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc19"><IMG SRC="toc.gif" ALT="Contents"></A>
<HR>
<H2><A NAME="s19">19. Non-local Exits</A></H2>
<P>The <CODE>return</CODE> special form mentioned in the section on iteration is an
example of a nonlocal return. Another example is the <CODE>return</CODE>-from form,
which returns a value from the surrounding function:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (defun foo (x)
(return-from foo 3)
x)
FOO
> (foo 17)
3
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Actually, the <CODE>return-from</CODE> 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:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (block foo
(return-from foo 7)
3)
7
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The <CODE>return</CODE> special form can return from any block named
<CODE>nil</CODE>. Loops are
by default labelled <CODE>nil</CODE>, but you can make your own nil-labelled blocks:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (block nil
(return 7)
3)
7
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>Another form which causes a nonlocal exit is the <CODE>error</CODE> form:</P>
<P>
<BLOCKQUOTE><CODE>
<PRE>
> (error "This is an error")
Error: This is an error
</PRE>
</CODE></BLOCKQUOTE>
</P>
<P>The <CODE>error</CODE> form applies format to its arguments, then places you in the
debugger.</P>
<HR>
<A HREF="LISP-tutorial-18.html"><IMG SRC="prev.gif" ALT="Previous"></A>
<A HREF="LISP-tutorial-20.html"><IMG SRC="next.gif" ALT="Next"></A>
<A HREF="LISP-tutorial.html#toc19"><IMG SRC="toc.gif" ALT="Contents"></A>
</BODY>
</HTML>
|