61 lines
5 KiB
HTML
61 lines
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: 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>"~[Siamese~;Manx~;Persian~] Cat"</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>"~[Siamese~;Manx~;Persian~:;Alley~] Cat"</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
|
|
"~@[ print level = ~D~]~@[ print length = ~D~]"
|
|
*print-level* *print-length*)
|
|
=> " print length = 5"
|
|
</PRE>
|
|
</TT> <P>
|
|
Note also that <P>
|
|
<PRE>
|
|
(format stream "...~@[str~]..." ...)
|
|
== (format stream "...~:[~;~:*str~]..." ...)
|
|
</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 "Items:~#[ none~; ~S~; ~S and ~S~
|
|
~:;~@{~#[~; and~] ~S~^ ,~}~].")
|
|
(format nil foo) => "Items: none."
|
|
(format nil foo 'foo) => "Items: FOO."
|
|
(format nil foo 'foo 'bar) => "Items: FOO and BAR."
|
|
(format nil foo 'foo 'bar 'baz) => "Items: FOO, BAR, and BAZ."
|
|
(format nil foo 'foo 'bar 'baz 'quux) => "Items: FOO, BAR, BAZ, and QUUX."
|
|
</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>
|