diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/elisp/cons-cells.html | |
new repository
Diffstat (limited to 'devdocs/elisp/cons-cells.html')
| -rw-r--r-- | devdocs/elisp/cons-cells.html | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/devdocs/elisp/cons-cells.html b/devdocs/elisp/cons-cells.html new file mode 100644 index 00000000..cbad9ebb --- /dev/null +++ b/devdocs/elisp/cons-cells.html @@ -0,0 +1,6 @@ + <h3 class="section">Lists and Cons Cells</h3> <p>Lists in Lisp are not a primitive data type; they are built up from <em>cons cells</em> (see <a href="cons-cell-type">Cons Cell Type</a>). A cons cell is a data object that represents an ordered pair. That is, it has two slots, and each slot <em>holds</em>, or <em>refers to</em>, some Lisp object. One slot is known as the <small>CAR</small>, and the other is known as the <small>CDR</small>. (These names are traditional; see <a href="cons-cell-type">Cons Cell Type</a>.) <small>CDR</small> is pronounced “could-er”. </p> <p>We say that “the <small>CAR</small> of this cons cell is” whatever object its <small>CAR</small> slot currently holds, and likewise for the <small>CDR</small>. </p> <p>A list is a series of cons cells chained together, so that each cell refers to the next one. There is one cons cell for each element of the list. By convention, the <small>CAR</small>s of the cons cells hold the elements of the list, and the <small>CDR</small>s are used to chain the list (this asymmetry between <small>CAR</small> and <small>CDR</small> is entirely a matter of convention; at the level of cons cells, the <small>CAR</small> and <small>CDR</small> slots have similar properties). Hence, the <small>CDR</small> slot of each cons cell in a list refers to the following cons cell. </p> <p>Also by convention, the <small>CDR</small> of the last cons cell in a list is <code>nil</code>. We call such a <code>nil</code>-terminated structure a <em>proper list</em><a id="DOCF4" href="#FOOT4"><sup>4</sup></a>. In Emacs Lisp, the symbol <code>nil</code> is both a symbol and a list with no elements. For convenience, the symbol <code>nil</code> is considered to have <code>nil</code> as its <small>CDR</small> (and also as its <small>CAR</small>). </p> <p>Hence, the <small>CDR</small> of a proper list is always a proper list. The <small>CDR</small> of a nonempty proper list is a proper list containing all the elements except the first. </p> <p>If the <small>CDR</small> of a list’s last cons cell is some value other than <code>nil</code>, we call the structure a <em>dotted list</em>, since its printed representation would use dotted pair notation (see <a href="dotted-pair-notation">Dotted Pair Notation</a>). There is one other possibility: some cons cell’s <small>CDR</small> could point to one of the previous cons cells in the list. We call that structure a <em>circular list</em>. </p> <p>For some purposes, it does not matter whether a list is proper, circular or dotted. If a program doesn’t look far enough down the list to see the <small>CDR</small> of the final cons cell, it won’t care. However, some functions that operate on lists demand proper lists and signal errors if given a dotted list. Most functions that try to find the end of a list enter infinite loops if given a circular list. </p> <p>Because most cons cells are used as part of lists, we refer to any structure made out of cons cells as a <em>list structure</em>. </p><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/Cons-Cells.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Cons-Cells.html</a> + </p> +</div> |
