summaryrefslogtreecommitdiff
path: root/devdocs/elisp/setcdr.html
blob: cd7fe80e660c76de85cac2b4d32ec24ad52e0ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
 <h4 class="subsection">Altering the CDR of a List</h4>  <p>The lowest-level primitive for modifying a <small>CDR</small> is <code>setcdr</code>: </p> <dl> <dt id="setcdr">Function: <strong>setcdr</strong> <em>cons object</em>
</dt> <dd><p>This function stores <var>object</var> as the new <small>CDR</small> of <var>cons</var>, replacing its previous <small>CDR</small>. In other words, it changes the <small>CDR</small> slot of <var>cons</var> to refer to <var>object</var>. It returns the value <var>object</var>. </p></dd>
</dl> <p>Here is an example of replacing the <small>CDR</small> of a list with a different list. All but the first element of the list are removed in favor of a different sequence of elements. The first element is unchanged, because it resides in the <small>CAR</small> of the list, and is not reached via the <small>CDR</small>. </p> <div class="example"> <pre class="example">(setq x (list 1 2 3))
     ⇒ (1 2 3)
</pre>
<pre class="example">(setcdr x '(4))
     ⇒ (4)
</pre>
<pre class="example">x
     ⇒ (1 4)
</pre>
</div> <p>You can delete elements from the middle of a list by altering the <small>CDR</small>s of the cons cells in the list. For example, here we delete the second element, <code>b</code>, from the list <code>(a b c)</code>, by changing the <small>CDR</small> of the first cons cell: </p> <div class="example"> <pre class="example">(setq x1 (list 'a 'b 'c))
     ⇒ (a b c)
(setcdr x1 (cdr (cdr x1)))
     ⇒ (c)
x1
     ⇒ (a c)
</pre>
</div> <p>Here is the result in box notation: </p> <div class="example"> <pre class="example">                   --------------------
                  |                    |
 --------------   |   --------------   |    --------------
| car   | cdr  |  |  | car   | cdr  |   --&gt;| car   | cdr  |
|   a   |   o-----   |   b   |   o--------&gt;|   c   |  nil |
|       |      |     |       |      |      |       |      |
 --------------       --------------        --------------
</pre>
</div> <p>The second cons cell, which previously held the element <code>b</code>, still exists and its <small>CAR</small> is still <code>b</code>, but it no longer forms part of this list. </p> <p>It is equally easy to insert a new element by changing <small>CDR</small>s: </p> <div class="example"> <pre class="example">(setq x1 (list 'a 'b 'c))
     ⇒ (a b c)
(setcdr x1 (cons 'd (cdr x1)))
     ⇒ (d b c)
x1
     ⇒ (a d b c)
</pre>
</div> <p>Here is this result in box notation: </p> <div class="example"> <pre class="example"> --------------        -------------       -------------
| car  | cdr   |      | car  | cdr  |     | car  | cdr  |
|   a  |   o   |   --&gt;|   b  |   o-------&gt;|   c  |  nil |
|      |   |   |  |   |      |      |     |      |      |
 --------- | --   |    -------------       -------------
           |      |
     -----         --------
    |                      |
    |    ---------------   |
    |   | car   | cdr   |  |
     --&gt;|   d   |   o------
        |       |       |
         ---------------
</pre>
</div><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Setcdr.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Setcdr.html</a>
  </p>
</div>