diff options
Diffstat (limited to 'devdocs/elisp/setcdr.html')
| -rw-r--r-- | devdocs/elisp/setcdr.html | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/devdocs/elisp/setcdr.html b/devdocs/elisp/setcdr.html new file mode 100644 index 00000000..cd7fe80e --- /dev/null +++ b/devdocs/elisp/setcdr.html @@ -0,0 +1,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 | -->| car | cdr | +| a | o----- | b | o-------->| 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 | -->| b | o------->| c | nil | +| | | | | | | | | | | + --------- | -- | ------------- ------------- + | | + ----- -------- + | | + | --------------- | + | | car | cdr | | + -->| d | o------ + | | | + --------------- +</pre> +</div><div class="_attribution"> + <p class="_attribution-p"> + Copyright © 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> |
