119 lines
6.8 KiB
HTML
119 lines
6.8 KiB
HTML
|
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
||
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
||
|
<HEAD>
|
||
|
<TITLE>15.1. Conses</TITLE>
|
||
|
</HEAD>
|
||
|
<BODY>
|
||
|
<meta name="description" value=" Conses">
|
||
|
<meta name="keywords" value="clm">
|
||
|
<meta name="resource-type" value="document">
|
||
|
<meta name="distribution" value="global">
|
||
|
<P>
|
||
|
<b>Common Lisp the Language, 2nd Edition</b>
|
||
|
<BR> <HR><A NAME=tex2html3377 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3375 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3369 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3379 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3380 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3378 HREF="node149.html"> Lists</A>
|
||
|
<B>Up:</B> <A NAME=tex2html3376 HREF="node147.html"> Lists</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3370 HREF="node147.html"> Lists</A>
|
||
|
<HR> <P>
|
||
|
<H1><A NAME=SECTION001910000000000000000>15.1. Conses</A></H1>
|
||
|
<P>
|
||
|
These are the basic operations on conses viewed as pairs rather than
|
||
|
as the constituents of a list.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>car <i>list</i></tt><P>This returns the <i>car</i> of <i>list</i>, which must be a cons or <tt>()</tt>;
|
||
|
that is, <i>list</i> must satisfy the predicate <tt>listp</tt>.
|
||
|
By definition, the <i>car</i> of <tt>()</tt> is <tt>()</tt>.
|
||
|
If the cons is regarded as the first cons of a list, then <tt>car</tt>
|
||
|
returns the first element of the list.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(car '(a b c)) => a
|
||
|
</pre><P>
|
||
|
See <tt>first</tt>.
|
||
|
The <i>car</i> of a cons may be altered by using <tt>rplaca</tt> or <tt>setf</tt>.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>cdr <i>list</i></tt><P>This returns the <i>cdr</i> of <i>list</i>, which must be a cons or <tt>()</tt>;
|
||
|
that is, <i>list</i> must satisfy the predicate <tt>listp</tt>.
|
||
|
By definition, the <i>cdr</i> of <tt>()</tt> is <tt>()</tt>.
|
||
|
If the cons is regarded as the first cons of a list, then <tt>cdr</tt>
|
||
|
returns the rest of the list, which is a list with all elements
|
||
|
but the first of the original list.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(cdr '(a b c)) => (b c)
|
||
|
</pre><P>
|
||
|
See <tt>rest</tt>.
|
||
|
The <i>cdr</i> of a cons may be altered by using <tt>rplacd</tt> or <tt>setf</tt>.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>caar <i>list</i> <BR></tt><tt>cadr <i>list</i> <BR></tt><tt>cdar <i>list</i> <BR></tt><tt>cddr <i>list</i> <BR></tt><tt>caaar <i>list</i> <BR></tt><tt>caadr <i>list</i> <BR></tt><tt>cadar <i>list</i> <BR></tt><tt>caddr <i>list</i> <BR></tt><tt>cdaar <i>list</i> <BR></tt><tt>cdadr <i>list</i> <BR></tt><tt>cddar <i>list</i> <BR></tt><tt>cdddr <i>list</i> <BR></tt><tt>caaaar <i>list</i> <BR></tt><tt>caaadr <i>list</i> <BR></tt><tt>caadar <i>list</i> <BR></tt><tt>caaddr <i>list</i> <BR></tt><tt>cadaar <i>list</i> <BR></tt><tt>cadadr <i>list</i> <BR></tt><tt>caddar <i>list</i> <BR></tt><tt>cadddr <i>list</i> <BR></tt><tt>cdaaar <i>list</i> <BR></tt><tt>cdaadr <i>list</i> <BR></tt><tt>cdadar <i>list</i> <BR></tt><tt>cdaddr <i>list</i> <BR></tt><tt>cddaar <i>list</i> <BR></tt><tt>cddadr <i>list</i> <BR></tt><tt>cdddar <i>list</i> <BR></tt><tt>cddddr <i>list</i></tt><P>All of the compositions of up to four <tt>car</tt> and <tt>cdr</tt> operations
|
||
|
are defined as separate Common Lisp functions.
|
||
|
The names of these functions begin with <tt>c</tt> and end with <tt>r</tt>,
|
||
|
and in between is a sequence of <tt>a</tt> and <tt>d</tt> letters
|
||
|
corresponding to
|
||
|
the composition performed by the function.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(cddadr x) is the same as (cdr (cdr (car (cdr x))))
|
||
|
</pre><P>
|
||
|
If the argument is regarded as a list, then <tt>cadr</tt> returns
|
||
|
the second element of the list, <tt>caddr</tt> the third, and <tt>cadddr</tt>
|
||
|
the fourth. If the first element of a list is a list, then
|
||
|
<tt>caar</tt> is the first element of the sublist, <tt>cdar</tt> is the
|
||
|
rest of that sublist, and <tt>cadar</tt> is the second element of the sublist,
|
||
|
and so on.
|
||
|
<P>
|
||
|
As a matter of style, it is often preferable to define a function or
|
||
|
macro to access part of a complicated data structure, rather than to use
|
||
|
a long <tt>car</tt>/<tt>cdr</tt> string. For example, one might define
|
||
|
a macro to extract the list of parameter variables from a lambda-expression:
|
||
|
<P><pre>
|
||
|
(defmacro lambda-vars (lambda-exp) `(cadr ,lambda-exp))
|
||
|
</pre><P>
|
||
|
and then use <tt>lambda-vars</tt> for this purpose instead of <tt>cadr</tt>.
|
||
|
See also <tt>defstruct</tt>, which will automatically define
|
||
|
new record data types and access functions for instances of them.
|
||
|
<P>
|
||
|
Any of these functions may be used to specify a <i>place</i> for <tt>setf</tt>.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>cons <i>x</i> <i>y</i></tt><P><tt>cons</tt> is the primitive function to create a new <i>cons</i> whose
|
||
|
<i>car</i> is <i>x</i> and whose <i>cdr</i> is <i>y</i>.
|
||
|
For example:
|
||
|
<P><pre>
|
||
|
(cons 'a 'b) => (a . b)
|
||
|
(cons 'a (cons 'b (cons 'c '<tt>()</tt>))) => (a b c)
|
||
|
(cons 'a '(b c d)) => (a b c d)
|
||
|
</pre><P>
|
||
|
<tt>cons</tt> may be thought of as creating a <i>cons</i>, or as adding a new element
|
||
|
to the front of a list.
|
||
|
<P>
|
||
|
<BR><b>[Function]</b><BR>
|
||
|
<tt>tree-equal <i>x</i> <i>y</i> &key :test :test-not</tt><P>This is a predicate that is true if <i>x</i> and <i>y</i> are
|
||
|
isomorphic trees with identical leaves, that is, if <i>x</i> and <i>y</i>
|
||
|
are atoms that satisfy the test (by default <tt>eql</tt>),
|
||
|
or if they are both conses and their <i>car</i>'s are <tt>tree-equal</tt>
|
||
|
and their <i>cdr</i>'s are <tt>tree-equal</tt>.
|
||
|
Thus <tt>tree-equal</tt> recursively compares conses (but not any other objects
|
||
|
that have components). See <tt>equal</tt>, which does recursively
|
||
|
compare certain other structured objects, such as strings.
|
||
|
<P>
|
||
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
||
|
X3J13 voted in January 1989
|
||
|
(MAPPING-DESTRUCTIVE-INTERACTION) <A NAME=16271> </A>
|
||
|
to restrict user side effects; see section <A HREF="node92.html#STRUCTURETRAVERSALSECTION">7.9</A>.
|
||
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
||
|
<P>
|
||
|
<BR> <HR><A NAME=tex2html3377 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3375 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3369 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3379 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3380 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
||
|
<B> Next:</B> <A NAME=tex2html3378 HREF="node149.html"> Lists</A>
|
||
|
<B>Up:</B> <A NAME=tex2html3376 HREF="node147.html"> Lists</A>
|
||
|
<B> Previous:</B> <A NAME=tex2html3370 HREF="node147.html"> Lists</A>
|
||
|
<HR> <P>
|
||
|
<HR>
|
||
|
<P><ADDRESS>
|
||
|
AI.Repository@cs.cmu.edu
|
||
|
</ADDRESS>
|
||
|
</BODY>
|