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

87 lines
8 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 2.4.6</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="02_de.htm">
<LINK REL=UP HREF="02_d.htm">
<LINK REL=NEXT HREF="02_dfa.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="02_de.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="02_d.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="02_dfa.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
2.4.6 Backquote</H2> <P>
The <A REL=DEFINITION HREF="26_glo_b.htm#backquote"><I>backquote</I></A> introduces a template of a data structure to be built. For example, writing <P>
<PRE>
`(cond ((numberp ,x) ,@y) (t (print ,x) ,@y))
</PRE>
</TT> is roughly equivalent to writing <P>
<PRE>
(list 'cond
(cons (list 'numberp x) y)
(list* 't (list 'print x) y))
</PRE>
</TT> Where a comma occurs in the template, the <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A> following the comma is to be evaluated to produce an <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> to be inserted at that point. Assume <TT>b</TT> has the value 3, for example, then evaluating the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> denoted by <TT>`(a b ,b ,(+ b 1) b)</TT> produces the result <TT>(a b 3 4 b)</TT>. <P>
If a comma is immediately followed by an <A REL=DEFINITION HREF="26_glo_a.htm#at-sign"><I>at-sign</I></A>, then the <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> following the <A REL=DEFINITION HREF="26_glo_a.htm#at-sign"><I>at-sign</I></A> is evaluated to produce a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> of <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A>. These <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>objects</I></A> are then ``spliced'' into place in the template. For example, if <TT>x</TT> has the value <TT>(a b c)</TT>, then <P>
<PRE>
`(x ,x ,@x foo ,(cadr x) bar ,(cdr x) baz ,@(cdr x))
=&gt; (x (a b c) a b c foo b bar (b c) baz b c)
</PRE>
</TT> The backquote syntax can be summarized formally as follows. <P>
<P><DL><DT>* <TT>`</TT><I>basic</I><TT></TT> is the same as <TT>'</TT><I>basic</I><TT></TT>, that is, <TT>(quote </TT><I>basic</I><TT>)</TT>, for any <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A> <I>basic</I> that is not a <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> or a general <A REL=DEFINITION HREF="26_glo_v.htm#vector"><I>vector</I></A>. <P><DD>
<DT>* <TT>`,</TT><I>form</I><TT></TT> is the same as <I>form</I>, for any <I>form</I>, provided that the representation of <I>form</I> does not begin with <A REL=DEFINITION HREF="26_glo_a.htm#at-sign"><I>at-sign</I></A> or <A REL=DEFINITION HREF="26_glo_d.htm#dot"><I>dot</I></A>. (A similar caveat holds for all occurrences of a form after a <A REL=DEFINITION HREF="26_glo_c.htm#comma"><I>comma</I></A>.) <P><DD>
<DT>* <TT>`,@</TT><I>form</I><TT></TT> has undefined consequences. <P><DD>
<DT>* <TT>`(x1 x2 x3 ... xn . atom)</TT> may be interpreted to mean <P><DD>
<PRE>
(append [ x1] [ x2] [ x3] ... [ xn] (quote atom))
</PRE>
</TT> where the brackets are used to indicate a transformation of an <I>xj</I> as follows: <P>
<P><DL><DT>-- <TT>[</TT><I>form</I><TT>]</TT> is interpreted as <TT>(list `</TT><I>form</I><TT>)</TT>, which contains a backquoted form that must then be further interpreted. <P><DD>
<DT>-- <TT>[,</TT><I>form</I><TT>]</TT> is interpreted as <TT>(list </TT><I>form</I><TT>)</TT>. <P><DD>
<DT>-- <TT>[,@</TT><I>form</I><TT>]</TT> is interpreted as <I>form</I>. <P><DD></DL><P>
<DT>* <TT>`(x1 x2 x3 ... xn)</TT> may be interpreted to mean the same as the backquoted form <TT>`(x1 x2 x3 ... xn . <A REL=DEFINITION HREF="a_nil.htm#nil"></TT><B>nil</B><TT></A>)</TT>, thereby reducing it to the previous case. <P><DD>
<DT>* <TT>`(x1 x2 x3 ... xn . ,form)</TT> may be interpreted to mean <P><DD>
<PRE>
(append [ x1] [ x2] [ x3] ... [ xn] form)
</PRE>
</TT> where the brackets indicate a transformation of an <TT>xj</TT> as described above. <P>
<DT>* <TT>`(x1 x2 x3 ... xn . ,@form)</TT> has undefined consequences. <P><DD>
<DT>* <TT>`#(x1 x2 x3 ... xn)</TT> may be interpreted to mean <TT>(apply #'vector `(x1 x2 x3 ... xn))</TT>. <P><DD></DL><P>
Anywhere ``<TT>,@</TT>'' may be used, the syntax ``<TT>,.</TT>'' may be used instead to indicate that it is permissible to operate <I>destructively</I> on the <A REL=DEFINITION HREF="26_glo_l.htm#list_structure"><I>list structure</I></A> produced by the form following the ``<TT>,.</TT>'' (in effect, to use <A REL=DEFINITION HREF="f_nconc.htm#nconc"><B>nconc</B></A> instead of <A REL=DEFINITION HREF="f_append.htm#append"><B>append</B></A>). <P>
If the backquote syntax is nested, the innermost backquoted form should be expanded first. This means that if several commas occur in a row, the leftmost one belongs to the innermost <A REL=DEFINITION HREF="26_glo_b.htm#backquote"><I>backquote</I></A>. <P>
An <A REL=DEFINITION HREF="26_glo_i.htm#implementation"><I>implementation</I></A> is free to interpret a backquoted <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> F1 as any <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> F2 that, when evaluated, will produce a result that is the <A REL=DEFINITION HREF="26_glo_s.htm#same"><I>same</I></A> under <A REL=DEFINITION HREF="f_equal.htm#equal"><B>equal</B></A> as the result implied by the above definition, provided that the side-effect behavior of the substitute <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> F2 is also consistent with the description given above. The constructed copy of the template might or might not share <A REL=DEFINITION HREF="26_glo_l.htm#list"><I>list</I></A> structure with the template itself. As an example, the above definition implies that <P>
<PRE>
`((,a b) ,c ,@d)
</PRE>
</TT> will be interpreted as if it were <P>
<PRE>
(append (list (append (list a) (list 'b) '<A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>)) (list c) d '<A REL=DEFINITION HREF="a_nil.htm#nil"><B>nil</B></A>)
</PRE>
</TT> but it could also be legitimately interpreted to mean any of the following: <P>
<PRE>
(append (list (append (list a) (list 'b))) (list c) d)
(append (list (append (list a) '(b))) (list c) d)
(list* (cons a '(b)) c d)
(list* (cons a (list 'b)) c d)
(append (list (cons a '(b))) (list c) d)
(list* (cons a '(b)) c (copy-list d))
</PRE>
</TT> <P>
<A REL=CHILD HREF="02_dfa.htm"><H2>
2.4.6.1 Notes about Backquote</H2></A><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>