File: LISP-tutorial-19.html

package info (click to toggle)
cmucl 20c-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 42,524 kB
  • sloc: lisp: 358,331; ansic: 28,385; asm: 3,777; sh: 1,236; makefile: 366; csh: 31
file content (72 lines) | stat: -rw-r--r-- 1,872 bytes parent folder | download | duplicates (12)
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>
&gt; (defun foo (x)
    (return-from foo 3)
    x)
FOO
&gt; (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>
&gt; (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>
&gt; (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>
&gt; (error &quot;This is an error&quot;)
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>