1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/m_loop_f.htm
2024-04-01 10:24:07 +02:00

93 lines
6.1 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: Local Macro LOOP-FINISH</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="m_loop.htm">
<LINK REL=UP HREF="c_iterat.htm">
<LINK REL=NEXT HREF="07_.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="m_loop.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_iterat.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="07_.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="loop-finish"><I>Local Macro</I> <B>LOOP-FINISH</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<B>loop-finish</B> <I><I>&lt;no arguments&gt;</I></I> =&gt;| <P>
<P>
<P><B>Description:</B><P>
<P>
The <A REL=DEFINITION HREF="#loop-finish"><B>loop-finish</B></A> <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A> can be used lexically within an extended <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> to terminate that <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> ``normally.'' That is, it transfers control to the loop epilogue of the lexically innermost extended <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. This permits execution of any <B>finally</B> clause (for effect) and the return of any accumulated result. <P>
<P><B>Examples:</B><P>
<P>
<PRE>
;; Terminate the loop, but return the accumulated count.
(loop for i in '(1 2 3 stop-here 4 5 6)
when (symbolp i) do (loop-finish)
count i)
=&gt; 3
;; The preceding loop is equivalent to:
(loop for i in '(1 2 3 stop-here 4 5 6)
until (symbolp i)
count i)
=&gt; 3
;; While LOOP-FINISH can be used can be used in a variety of
;; situations it is really most needed in a situation where a need
;; to exit is detected at other than the loop's `top level'
;; (where UNTIL or WHEN often work just as well), or where some
;; computation must occur between the point where a need to exit is
;; detected and the point where the exit actually occurs. For example:
(defun tokenize-sentence (string)
(macrolet ((add-word (wvar svar)
`(when ,wvar
(push (coerce (nreverse ,wvar) 'string) ,svar)
(setq ,wvar nil))))
(loop with word = '() and sentence = '() and endpos = nil
for i below (length string)
do (let ((char (aref string i)))
(case char
(#\Space (add-word word sentence))
(#\. (setq endpos (1+ i)) (loop-finish))
(otherwise (push char word))))
finally (add-word word sentence)
(return (values (nreverse sentence) endpos)))))
=&gt; TOKENIZE-SENTENCE
(tokenize-sentence &quot;this is a sentence. this is another sentence.&quot;)
=&gt; (&quot;this&quot; &quot;is&quot; &quot;a&quot; &quot;sentence&quot;), 19
(tokenize-sentence &quot;this is a sentence&quot;)
=&gt; (&quot;this&quot; &quot;is&quot; &quot;a&quot; &quot;sentence&quot;), NIL
</PRE>
</TT> <P>
<P><B>Side Effects:</B><P>
<P>
Transfers control. <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B><P>
<P>
Whether or not <A REL=DEFINITION HREF="#loop-finish"><B>loop-finish</B></A> is <A REL=DEFINITION HREF="26_glo_f.htm#fbound"><I>fbound</I></A> in the <A REL=DEFINITION HREF="26_glo_g.htm#global_environment"><I>global environment</I></A> is <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent"><I>implementation-dependent</I></A>; however, the restrictions on redefinition and <I>shadowing</I> of <A REL=DEFINITION HREF="#loop-finish"><B>loop-finish</B></A> are the same as for <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbols</I></A> in the <TT>COMMON-LISP</TT> package which are <A REL=DEFINITION HREF="26_glo_f.htm#fbound"><I>fbound</I></A> in the <A REL=DEFINITION HREF="26_glo_g.htm#global_environment"><I>global environment</I></A>. The consequences of attempting to use <A REL=DEFINITION HREF="#loop-finish"><B>loop-finish</B></A> outside of <A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A> are undefined. <P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="m_loop.htm#loop"><B>loop</B></A>, <A REL=CHILD HREF="06_a.htm">Section 6.1 (The LOOP Facility)</A> <P>
<P><B>Notes:</B><P>
<P>
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issue</A>, <I>not part of the specification</I>, applies to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss211.htm">LEXICAL-CONSTRUCT-GLOBAL-DEFINITION:UNDEFINED</A><P></UL><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>