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

68 lines
6.4 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 22.3.7.4</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="22_cgc.htm">
<LINK REL=UP HREF="22_cg.htm">
<LINK REL=NEXT HREF="22_cge.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="22_cgc.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="22_cg.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="22_cge.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
22.3.7.4 Tilde Left-Brace: Iteration</H2> <P>
<TT>~{</TT><I>str</I><TT>~}</TT> <P>
This is an iteration construct. The argument should be a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A>, which is used as a set of arguments as if for a recursive call to <A REL=DEFINITION HREF="f_format.htm#format"><B>format</B></A>. The <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A> <I>str</I> is used repeatedly as the control string. Each iteration can absorb as many elements of the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> as it likes as arguments; if <I>str</I> uses up two arguments by itself, then two elements of the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> will get used up each time around the loop. If before any iteration step the <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> is empty, then the iteration is terminated. Also, if a prefix parameter <I>n</I> is given, then there will be at most <I>n</I> repetitions of processing of <I>str</I>. Finally, the <TT>~^</TT> directive can be used to terminate the iteration prematurely. <P>
For example: <P>
<PRE>
(format nil &quot;The winners are:~{ ~S~}.&quot;
'(fred harry jill))
=&gt; &quot;The winners are: FRED HARRY JILL.&quot;
(format nil &quot;Pairs:~{ &lt;~S,~S&gt;~}.&quot;
'(a 1 b 2 c 3))
=&gt; &quot;Pairs: &lt;A,1&gt; &lt;B,2&gt; &lt;C,3&gt;.&quot;
</PRE>
</TT> <P>
<TT>~:</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT> is similar, but the argument should be a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of sublists. At each repetition step, one sublist is used as the set of arguments for processing <I>str</I>; on the next repetition, a new sublist is used, whether or not all of the last sublist had been processed. For example: <P>
<PRE>
(format nil &quot;Pairs:~:{ &lt;~S,~S&gt;~} .&quot;
'((a 1) (b 2) (c 3)))
=&gt; &quot;Pairs: &lt;A,1&gt; &lt;B,2&gt; &lt;C,3&gt;.&quot;
</PRE>
</TT> <P>
<TT>~@</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT> is similar to <TT>~</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT>, but instead of using one argument that is a list, all the remaining arguments are used as the list of arguments for the iteration. Example: <P>
<PRE>
(format nil &quot;Pairs:~@{ &lt;~S,~S&gt;~} .&quot; 'a 1 'b 2 'c 3)
=&gt; &quot;Pairs: &lt;A,1&gt; &lt;B,2&gt; &lt;C,3&gt;.&quot;
</PRE>
</TT> If the iteration is terminated before all the remaining arguments are consumed, then any arguments not processed by the iteration remain to be processed by any directives following the iteration construct. <P>
<TT>~:@</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT> combines the features of <TT>~:</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT> and <TT>~@</TT>{<TT></TT><I>str</I><TT>~</TT>}<TT></TT>. All the remaining arguments are used, and each one must be a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A>. On each iteration, the next argument is used as a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of arguments to <I>str</I>. Example: <P>
<PRE>
(format nil &quot;Pairs:~:@{ &lt;~S,~S&gt;~} .&quot;
'(a 1) '(b 2) '(c 3))
=&gt; &quot;Pairs: &lt;A,1&gt; &lt;B,2&gt; &lt;C,3&gt;.&quot;
</PRE>
</TT> Terminating the repetition construct with <TT>~:</TT>}<TT></TT> instead of <TT>~</TT>}<TT></TT> forces <I>str</I> to be processed at least once, even if the initial list of arguments is null. However, this will not override an explicit prefix parameter of zero. <P>
If <I>str</I> is empty, then an argument is used as <I>str</I>. It must be a <A REL=DEFINITION HREF="26_glo_f.htm#format_control"><I>format control</I></A> and precede any arguments processed by the iteration. As an example, the following are equivalent: <P>
<PRE>
(apply #'format stream string arguments)
== (format stream &quot;~1{~:}&quot; string arguments)
</PRE>
</TT> <P>
This will use <TT>string</TT> as a formatting string. The <TT>~1</TT>{<TT></TT> says it will be processed at most once, and the <TT>~:</TT>}<TT></TT> says it will be processed at least once. Therefore it is processed exactly once, using <TT>arguments</TT> as the arguments. This case may be handled more clearly by the <TT>~?</TT> directive, but this general feature of <TT>~</TT>{<TT></TT> is more powerful than <TT>~?</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>