50 lines
6 KiB
HTML
50 lines
6 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Section 3.1.6</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="03_ae.htm">
|
|
<LINK REL=UP HREF="03_a.htm">
|
|
<LINK REL=NEXT HREF="03_ag.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="03_ae.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="03_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="03_ag.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<H2>
|
|
3.1.6 Extent</H2> <TT>Contorted-example</TT> works only because the <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> named by <TT>f</TT> is invoked during the <A REL=DEFINITION HREF="26_glo_e.htm#extent"><I>extent</I></A> of the <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A>. Once the flow of execution has left the block, the <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> is <I>disestablished</I>. For example: <P>
|
|
<PRE>
|
|
(defun invalid-example ()
|
|
(let ((y (block here #'(lambda (z) (return-from here z)))))
|
|
(if (numberp y) y (funcall y 5))))
|
|
</PRE>
|
|
</TT> One might expect the call <TT>(invalid-example)</TT> to produce <TT>5</TT> by the following incorrect reasoning: <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A> binds <TT>y</TT> to the value of <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A>; this value is a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> resulting from the <A REL=DEFINITION HREF="26_glo_l.htm#lambda_expression"><I>lambda expression</I></A>. Because <TT>y</TT> is not a number, it is invoked on the value <TT>5</TT>. The <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> should then return this value from the <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> named <TT>here</TT>, thereby exiting from the block again and giving <TT>y</TT> the value <TT>5</TT> which, being a number, is then returned as the value of the call to <TT>invalid-example</TT>. <P>
|
|
The argument fails only because <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit points</I></A> have <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_extent"><I>dynamic extent</I></A>. The argument is correct up to the execution of <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A>. The execution of <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> should signal an error of <A REL=DEFINITION HREF="26_glo_t.htm#type"><I>type</I></A> <A REL=DEFINITION HREF="e_contro.htm#control-error"><B>control-error</B></A>, however, not because it cannot refer to the <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A>, but because it does correctly refer to an <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> and that <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> has been <I>disestablished</I>. <P>
|
|
A reference by name to a dynamic <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> binding such as a <A REL=DEFINITION HREF="26_glo_c.htm#catch_tag"><I>catch tag</I></A> refers to the most recently <I>established</I> <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of that name that has not been <I>disestablished</I>. For example: <P>
|
|
<PRE>
|
|
(defun fun1 (x)
|
|
(catch 'trap (+ 3 (fun2 x))))
|
|
(defun fun2 (y)
|
|
(catch 'trap (* 5 (fun3 y))))
|
|
(defun fun3 (z)
|
|
(throw 'trap z))
|
|
</PRE>
|
|
</TT> Consider the call <TT>(fun1 7)</TT>. The result is <TT>10</TT>. At the time the <A REL=DEFINITION HREF="s_throw.htm#throw"><B>throw</B></A> is executed, there are two outstanding catchers with the name <TT>trap</TT>: one established within procedure <TT>fun1</TT>, and the other within procedure <TT>fun2</TT>. The latter is the more recent, and so the value <TT>7</TT> is returned from <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> in <TT>fun2</TT>. Viewed from within <TT>fun3</TT>, the <A REL=DEFINITION HREF="s_catch.htm#catch"><B>catch</B></A> in <TT>fun2</TT> shadows the one in <TT>fun1</TT>. Had <TT>fun2</TT> been defined as <P>
|
|
<PRE>
|
|
(defun fun2 (y)
|
|
(catch 'snare (* 5 (fun3 y))))
|
|
</PRE>
|
|
</TT> then the two <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit points</I></A> would have different <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>names</I></A>, and therefore the one in <TT>fun1</TT> would not be shadowed. The result would then have been <TT>7</TT>. <P>
|
|
<HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|