1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/22_cgb.htm

62 lines
5 KiB
HTML
Raw Normal View History

2024-04-01 10:24:07 +02:00
<!-- 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.2</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_cga.htm">
<LINK REL=UP HREF="22_cg.htm">
<LINK REL=NEXT HREF="22_cgc.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_cga.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_cgc.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
22.3.7.2 Tilde Left-Bracket: Conditional Expression</H2> <P>
<TT>~[</TT><I>str0</I><TT>~;</TT><I>str1</I><TT>~;</TT><I>...</I><TT>~;</TT><I>strn</I><TT>~]</TT> <P>
This is a set of control strings, called <I>clauses</I>, one of which is chosen and used. The clauses are separated by <TT>~;</TT> and the construct is terminated by <TT>~]</TT>. For example, <P>
<TT>&quot;~[Siamese~;Manx~;Persian~] Cat&quot;</TT> <P>
The <I>arg</I>th clause is selected, where the first clause is number 0. If a prefix parameter is given (as <TT>~</TT><I>n</I><TT>[</TT>), then the parameter is used instead of an argument. If <I>arg</I> is out of range then no clause is selected and no error is signaled. After the selected alternative has been processed, the control string continues after the <TT>~]</TT>. <P>
<TT>~[</TT><I>str0</I><TT>~;</TT><I>str1</I><TT>~;</TT><I>...</I><TT>~;</TT><I>strn</I><TT>~:;</TT><I>default</I><TT>~]</TT> has a default case. If the <I>last</I> <TT>~;</TT> used to separate clauses is <TT>~:;</TT> instead, then the last clause is an else clause that is performed if no other clause is selected. For example: <P>
<TT>&quot;~[Siamese~;Manx~;Persian~:;Alley~] Cat&quot;</TT> <P>
<TT>~:[</TT><I>alternative</I><TT>~;</TT><I>consequent</I><TT>~]</TT> selects the <I>alternative</I> control string if <I>arg</I> is <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>, and selects the <I>consequent</I> control string otherwise. <P>
<TT>~@[</TT><I>consequent</I><TT>~]</TT> tests the argument. If it is <A REL=DEFINITION HREF="26_glo_t.htm#true"><I>true</I></A>, then the argument is not used up by the <TT>~[</TT> command but remains as the next one to be processed, and the one clause <I>consequent</I> is processed. If the <I>arg</I> is <A REL=DEFINITION HREF="26_glo_f.htm#false"><I>false</I></A>, then the argument is used up, and the clause is not processed. The clause therefore should normally use exactly one argument, and may expect it to be <A REL=DEFINITION HREF="26_glo_n.htm#non-nil"><I>non-nil</I></A>. For example: <P>
<PRE>
(setq *print-level* nil *print-length* 5)
(format nil
&quot;~@[ print level = ~D~]~@[ print length = ~D~]&quot;
*print-level* *print-length*)
=&gt; &quot; print length = 5&quot;
</PRE>
</TT> <P>
Note also that <P>
<PRE>
(format stream &quot;...~@[str~]...&quot; ...)
== (format stream &quot;...~:[~;~:*str~]...&quot; ...)
</PRE>
</TT> <P>
The combination of <TT>~[</TT> and <TT>#</TT> is useful, for example, for dealing with English conventions for printing lists: <P>
<PRE>
(setq foo &quot;Items:~#[ none~; ~S~; ~S and ~S~
~:;~@{~#[~; and~] ~S~^ ,~}~].&quot;)
(format nil foo) =&gt; &quot;Items: none.&quot;
(format nil foo 'foo) =&gt; &quot;Items: FOO.&quot;
(format nil foo 'foo 'bar) =&gt; &quot;Items: FOO and BAR.&quot;
(format nil foo 'foo 'bar 'baz) =&gt; &quot;Items: FOO, BAR, and BAZ.&quot;
(format nil foo 'foo 'bar 'baz 'quux) =&gt; &quot;Items: FOO, BAR, BAZ, and QUUX.&quot;
</PRE>
</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>