emacs.d/clones/lisp/www.cs.cmu.edu/Groups/AI/html/cltl/clm/node150.html

84 lines
4.4 KiB
HTML
Raw Normal View History

2022-08-26 19:11:35 +02:00
<!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.3. Alteration of List Structure</TITLE>
</HEAD>
<BODY>
<meta name="description" value=" Alteration of List Structure">
<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=tex2html3401 HREF="node151.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3399 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3393 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3403 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3404 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3402 HREF="node151.html"> Substitution of Expressions</A>
<B>Up:</B> <A NAME=tex2html3400 HREF="node147.html"> Lists</A>
<B> Previous:</B> <A NAME=tex2html3394 HREF="node149.html"> Lists</A>
<HR> <P>
<H1><A NAME=SECTION001930000000000000000>15.3. Alteration of List Structure</A></H1>
<P>
The functions <tt>rplaca</tt> and <tt>rplacd</tt>
may be used to make alterations in already existing
list structure, that is, to change the <i>car</i> or <i>cdr</i> of an
existing cons.
One may also use <tt>setf</tt> in conjunction with <tt>car</tt> and <tt>cdr</tt>.
<P>
The structure is not copied but is destructively altered;
hence caution should be exercised when using these functions, as
strange side effects can occur if portions of list structure become
shared.
The <tt>nconc</tt>, <tt>nreverse</tt>, <tt>nreconc</tt>,
and <tt>nbutlast</tt> functions, already
described,
have the same property, as do certain of the generic sequence
functions such as <tt>delete</tt>.
However, they are normally not
used for this side effect; rather, the list-structure modification
is purely for efficiency, and compatible non-modifying functions
are provided.
<P>
<BR><b>[Function]</b><BR>
<tt>rplaca <i>x</i> <i>y</i></tt><P><tt>(rplaca <i>x</i> <i>y</i>)</tt> changes the <i>car</i> of <i>x</i> to <i>y</i> and returns
(the modified) <i>x</i>. <i>x</i> must be a cons, but <i>y</i> may be any
Lisp object.
For example:
<P><pre>
(setq g '(a b c))
(rplaca (cdr g) 'd) => (d c)
Now g => (a d c)
</pre><P>
<P>
<BR><b>[Function]</b><BR>
<tt>rplacd <i>x</i> <i>y</i></tt><P><tt>(rplacd <i>x</i> <i>y</i>)</tt> changes the <i>cdr</i> of <i>x</i> to <i>y</i> and returns
(the modified) <i>x</i>. <i>x</i> must be a cons, but <i>y</i> may be
any Lisp object.
For example:
<P><pre>
(setq x '(a b c))
(rplacd x 'd) => (a . d)
Now x => (a . d)
</pre><P>
<P>
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
The functions <tt>rplaca</tt> and <tt>rplacd</tt> go back to the earliest
origins of Lisp, along with <tt>car</tt>, <tt>cdr</tt>, and <tt>cons</tt>.
Nowadays, however, they seem to be falling by the wayside.
More and more Common Lisp programmers use <tt>setf</tt> for nearly
all structure modifications: <tt>(rplaca x y)</tt> is rendered
as <tt>(setf (car x) y)</tt> or perhaps as <tt>(setf (first x) y)</tt>.
Even more likely is that a <tt>defstruct</tt> structure or a CLOS class
is used in place of a list, if the data structure is at all complicated;
in this case <tt>setf</tt> is used with a slot accessor.
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
<P>
<BR> <HR><A NAME=tex2html3401 HREF="node151.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html3399 HREF="node147.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html3393 HREF="node149.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html3403 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html3404 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
<B> Next:</B> <A NAME=tex2html3402 HREF="node151.html"> Substitution of Expressions</A>
<B>Up:</B> <A NAME=tex2html3400 HREF="node147.html"> Lists</A>
<B> Previous:</B> <A NAME=tex2html3394 HREF="node149.html"> Lists</A>
<HR> <P>
<HR>
<P><ADDRESS>
AI.Repository@cs.cmu.edu
</ADDRESS>
</BODY>