1
0
Fork 0
cl-sites/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node85.html
2023-10-25 11:23:21 +02:00

109 lines
6 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
<HEAD>
<TITLE>7.7. Blocks and Exits</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Blocks and Exits">
<meta name="keywords" value="clm">
<meta name="resource-type" value="document">
<meta name="distribution" value="global">
<P>
<b>Common Lisp the Language, 2nd Edition</b>
<BR> <HR><A NAME=tex2html2584 HREF="node86.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2582 HREF="node76.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2576 HREF="node84.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2586 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2587 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2585 HREF="node86.html"> Iteration</A>
<B>Up:</B> <A NAME=tex2html2583 HREF="node76.html"> Control Structure</A>
<B> Previous:</B> <A NAME=tex2html2577 HREF="node84.html"> Conditionals</A>
<HR> <P>
<H1><A NAME=SECTION001170000000000000000>7.7. Blocks and Exits</A></H1>
<P>
<A NAME=BLOCKRETURNSECTION>The</A>
<tt>block</tt> and <tt>return-from</tt> constructs provide a structured lexical
non-local exit facility. At any point lexically within a <tt>block</tt>
construct, a <tt>return-from</tt> with the same name may be used to
perform an immediate transfer of control that
exits from the <tt>block</tt>. In the most common cases this mechanism is
more efficient than the dynamic non-local exit facility
provided by <tt>catch</tt> and <tt>throw</tt>, described in
section <A HREF="node96.html#CATCHTHROWSECTION">7.11</A>.
<P>
<BR><b>[Special Form]</b><BR>
<tt>block <i>name</i> {<i>form</i>}*</tt><P>The <tt>block</tt> construct executes each <i>form</i> from left to right,
returning whatever is returned by the last <i>form</i>.
If, however, a <tt>return</tt> or <tt>return-from</tt> form that specifies the
same <i>name</i> is executed
during the execution of some <i>form</i>, then the results
specified by the <tt>return</tt> or <tt>return-from</tt> are immediately
returned as the value of the <tt>block</tt> construct, and execution
proceeds as if the <tt>block</tt> had terminated normally.
In this, <tt>block</tt> differs from <tt>progn</tt>; the <tt>progn</tt> construct
has nothing to do with <tt>return</tt>.
<P>
The <i>name</i> is not evaluated; it must be a symbol.
The scope of the <i>name</i> is lexical; only a <tt>return</tt> or <tt>return-from</tt>
textually contained in some <i>form</i> can exit from the block.
The extent of the name is dynamic.
Therefore it is only possible to exit from a given run-time incarnation of a
block once, either normally or by explicit return.
<P>
The <tt>defun</tt> form implicitly puts a <tt>block</tt> around the
body of the function defined; the <tt>block</tt> has the same name as the function.
Therefore one may use <tt>return-from</tt> to return
prematurely from a function defined by <tt>defun</tt>.
<P>
The lexical scoping of the block name
is fully general and has consequences that may be surprising
to users and implementors of other Lisp systems.
For example, the <tt>return-from</tt> in the following example actually does
work in Common Lisp as one might expect:
<P><pre>
(block loser
(catch 'stuff
(mapcar #'(lambda (x) (if (numberp x)
(hairyfun x)
(return-from loser <tt>nil</tt>)))
items)))
</pre><P>
Depending on the situation, a <tt>return</tt> in Common Lisp
may not be simple.
A <tt>return</tt> can break up catchers if necessary to get
to the block in question.
It is possible for a ``closure'' created by <tt>function</tt> for
a lambda-expression to refer to a block name as long as the name
is lexically apparent.
<P>
<BR><b>[Special Form]</b><BR>
<tt>return-from <i>name</i> [<i>result</i>]</tt><P><tt>return-from</tt>
is used to return from a <tt>block</tt> or from such constructs
as <tt>do</tt> and <tt>prog</tt> that implicitly establish a <tt>block</tt>.
The <i>name</i> is not evaluated and must be a symbol.
A <tt>block</tt> construct with the same name must lexically
enclose the occurrence of <tt>return-from</tt>;
whatever the evaluation of <i>result</i> produces
is immediately returned from the block.
(If the <i>result</i> form is omitted, it defaults to <tt>nil</tt>.
As a matter of style, this form ought to be used to indicate that
the particular value returned doesn't matter.)
<P>
The <tt>return-from</tt> form itself never returns and cannot have a value;
it causes results to be returned from a <tt>block</tt> construct.
If the evaluation of <i>result</i> produces multiple values,
those multiple values are returned by the construct exited.
<P>
<BR><b>[Macro]</b><BR>
<tt>return</tt> <tt>[<i>result</i>]</tt><P><tt>(return <i>form</i>)</tt> is identical in meaning
to <tt>(return-from <tt>nil</tt> <i>form</i>)</tt>; it returns from a block named <tt>nil</tt>.
Blocks established implicitly by iteration constructs such
as <tt>do</tt> are named <tt>nil</tt>, so that <tt>return</tt> will exit properly from
such a construct.
<P>
<BR> <HR><A NAME=tex2html2584 HREF="node86.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2582 HREF="node76.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2576 HREF="node84.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2586 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2587 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html2585 HREF="node86.html"> Iteration</A>
<B>Up:</B> <A NAME=tex2html2583 HREF="node76.html"> Control Structure</A>
<B> Previous:</B> <A NAME=tex2html2577 HREF="node84.html"> Conditionals</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>