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

128 lines
8.5 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: Macro PROG, PROG*</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_nth_va.htm">
<LINK REL=UP HREF="c_data_a.htm">
<LINK REL=NEXT HREF="m_prog1c.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_nth_va.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="m_prog1c.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="prog"><A NAME="progST"><I>Macro</I> <B>PROG, PROG*</B></A></A> <P>
<P>
<P><B>Syntax:</B><P>
<P>
<B>prog</B> <I>({<I>var</I> | (<I>var</I> [<I>init-form</I>])}*) <I>declaration</I><B>*</B> {<I>tag</I> | <I>statement</I>}*</I><P> =&gt; <I><I>result</I><B>*</B></I><P>
<P>
<B>prog*</B> <I>({<I>var</I> | (<I>var</I> [<I>init-form</I>])}*) <I>declaration</I><B>*</B> {<I>tag</I> | <I>statement</I>}*</I><P> =&gt; <I><I>result</I><B>*</B></I><P>
<P>
<P><B>Arguments and Values:</B><P>
<P>
<I>var</I>---variable name. <P>
<I>init-form</I>---a <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
<I>tag</I>---a <A REL=DEFINITION HREF="26_glo_g.htm#go_tag"><I>go tag</I></A>; not evaluated. <P>
<I>statement</I>---a <A REL=DEFINITION HREF="26_glo_c.htm#compound_form"><I>compound form</I></A>; evaluated as described below. <P>
<I>results</I>---<A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> if a <A REL=DEFINITION HREF="26_glo_n.htm#normal_return"><I>normal return</I></A> occurs, or else, if an <A REL=DEFINITION HREF="26_glo_e.htm#explicit_return"><I>explicit return</I></A> occurs, the <A REL=DEFINITION HREF="26_glo_v.htm#value"><I>values</I></A> that were transferred. <P>
<P><B>Description:</B><P>
<P>
Three distinct operations are performed by <A REL=DEFINITION HREF="#prog"><B>prog</B></A> and <A REL=DEFINITION HREF="#progST"><B>prog*</B></A>: they bind local variables, they permit use of the <A REL=DEFINITION HREF="m_return.htm#return"><B>return</B></A> statement, and they permit use of the <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A> statement. A typical <A REL=DEFINITION HREF="#prog"><B>prog</B></A> looks like this: <P>
<PRE>
(prog (var1 var2 (var3 init-form-3) var4 (var5 init-form-5))
declaration*
statement1
tag1
statement2
statement3
statement4
tag2
statement5
...
)
</PRE>
</TT> <P>
For <A REL=DEFINITION HREF="#prog"><B>prog</B></A>, <I>init-forms</I> are evaluated first, in the order in which they are supplied. The <I>vars</I> are then bound to the corresponding values in parallel. If no <I>init-form</I> is supplied for a given <I>var</I>, that <I>var</I> is bound to <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>. <P>
The body of <A REL=DEFINITION HREF="#prog"><B>prog</B></A> is executed as if it were a <A REL=DEFINITION HREF="s_tagbod.htm#tagbody"><B>tagbody</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; the <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A> statement can be used to transfer control to a <I>tag</I>. <I>Tags</I> label <I>statements</I>. <P>
<A REL=DEFINITION HREF="#prog"><B>prog</B></A> implicitly establishes a <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A> named <A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A> around the entire <A REL=DEFINITION HREF="#prog"><B>prog</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>, so that <A REL=DEFINITION HREF="m_return.htm#return"><B>return</B></A> can be used at any time to exit from the <A REL=DEFINITION HREF="#prog"><B>prog</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. <P>
The difference between <A REL=DEFINITION HREF="#progST"><B>prog*</B></A> and <A REL=DEFINITION HREF="#prog"><B>prog</B></A> is that in <A REL=DEFINITION HREF="#progST"><B>prog*</B></A> the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> and initialization of the <I>vars</I> is done <A REL=DEFINITION HREF="26_glo_s.htm#sequentially"><I>sequentially</I></A>, so that the <I>init-form</I> for each one can use the values of previous ones. <P>
<P><B>Examples:</B><P>
<PRE>
(prog* ((y z) (x (car y)))
(return x))
</PRE>
</TT> returns the <A REL=DEFINITION HREF="26_glo_c.htm#car"><I>car</I></A> of the value of <TT>z</TT>. <P>
<PRE>
(setq a 1) =&gt; 1
(prog ((a 2) (b a)) (return (if (= a b) '= '/=))) =&gt; /=
(prog* ((a 2) (b a)) (return (if (= a b) '= '/=))) =&gt; =
(prog () 'no-return-value) =&gt; NIL
</PRE>
</TT>
<PRE>
(defun king-of-confusion (w)
&quot;Take a cons of two lists and make a list of conses.
Think of this function as being like a zipper.&quot;
(prog (x y z) ;Initialize x, y, z to NIL
(setq y (car w) z (cdr w))
loop
(cond ((null y) (return x))
((null z) (go err)))
rejoin
(setq x (cons (cons (car y) (car z)) x))
(setq y (cdr y) z (cdr z))
(go loop)
err
(cerror &quot;Will self-pair extraneous items&quot;
&quot;Mismatch - gleep! ~S&quot; y)
(setq z y)
(go rejoin))) =&gt; KING-OF-CONFUSION
</PRE>
</TT> This can be accomplished more perspicuously as follows: <P>
<PRE>
(defun prince-of-clarity (w)
&quot;Take a cons of two lists and make a list of conses.
Think of this function as being like a zipper.&quot;
(do ((y (car w) (cdr y))
(z (cdr w) (cdr z))
(x '<TT>()</TT> (cons (cons (car y) (car z)) x)))
((null y) x)
(when (null z)
(cerror &quot;Will self-pair extraneous items&quot;
&quot;Mismatch - gleep! ~S&quot; y)
(setq z y)))) =&gt; PRINCE-OF-CLARITY
</PRE>
</TT> <P>
<P><B>Affected By:</B> None.
<P>
<P><B>Exceptional Situations:</B> None.
<P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A>, <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A>, <A REL=DEFINITION HREF="s_tagbod.htm#tagbody"><B>tagbody</B></A>, <A REL=DEFINITION HREF="s_go.htm#go"><B>go</B></A>, <A REL=DEFINITION HREF="m_return.htm#return"><B>return</B></A>, <A REL=CHILD HREF="03_a.htm">Section 3.1 (Evaluation)</A> <P>
<P><B>Notes:</B><P>
<A REL=DEFINITION HREF="#prog"><B>prog</B></A> can be explained in terms of <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A>, <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A>, and <A REL=DEFINITION HREF="s_tagbod.htm#tagbody"><B>tagbody</B></A> as follows: <P>
<PRE>
(prog variable-list declaration . body)
== (block nil (let variable-list declaration (tagbody . body)))
</PRE>
</TT> <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/iss097.htm">DECLS-AND-DOC</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>