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
|
<h4 class="subsection">Cons Cell and List Types</h4> <p>A <em>cons cell</em> is an object that consists of two slots, called the <small>CAR</small> slot and the <small>CDR</small> slot. Each slot can <em>hold</em> any Lisp object. We also 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 <em>list</em> is a series of cons cells, linked together so that the <small>CDR</small> slot of each cons cell holds either the next cons cell or the empty list. The empty list is actually the symbol <code>nil</code>. See <a href="lists">Lists</a>, for details. 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> <blockquote> <p>A note to C programmers: a Lisp list thus works as a <em>linked list</em> built up of cons cells. Because pointers in Lisp are implicit, we do not distinguish between a cons cell slot holding a value versus pointing to the value. </p>
</blockquote> <p>Because cons cells are so central to Lisp, we also have a word for an object which is not a cons cell. These objects are called <em>atoms</em>. </p> <p>The read syntax and printed representation for lists are identical, and consist of a left parenthesis, an arbitrary number of elements, and a right parenthesis. Here are examples of lists: </p> <div class="example"> <pre class="example">(A 2 "A") ; <span class="roman">A list of three elements.</span>
() ; <span class="roman">A list of no elements (the empty list).</span>
nil ; <span class="roman">A list of no elements (the empty list).</span>
("A ()") ; <span class="roman">A list of one element: the string <code>"A ()"</code>.</span>
(A ()) ; <span class="roman">A list of two elements: <code>A</code> and the empty list.</span>
(A nil) ; <span class="roman">Equivalent to the previous.</span>
((A B C)) ; <span class="roman">A list of one element</span>
; <span class="roman">(which is a list of three elements).</span>
</pre>
</div> <p>Upon reading, each object inside the parentheses becomes an element of the list. That is, a cons cell is made for each element. The <small>CAR</small> slot of the cons cell holds the element, and its <small>CDR</small> slot refers to the next cons cell of the list, which holds the next element in the list. The <small>CDR</small> slot of the last cons cell is set to hold <code>nil</code>. </p> <p>The names <small>CAR</small> and <small>CDR</small> derive from the history of Lisp. The original Lisp implementation ran on an IBM 704 computer which divided words into two parts, the address and the decrement; <small>CAR</small> was an instruction to extract the contents of the address part of a register, and <small>CDR</small> an instruction to extract the contents of the decrement. By contrast, cons cells are named for the function <code>cons</code> that creates them, which in turn was named for its purpose, the construction of cells. </p> <table class="menu" border="0" cellspacing="0"> <tr>
<td align="left" valign="top">• <a href="box-diagrams" accesskey="1">Box Diagrams</a>
</td>
<td> </td>
<td align="left" valign="top">Drawing pictures of lists. </td>
</tr> <tr>
<td align="left" valign="top">• <a href="dotted-pair-notation" accesskey="2">Dotted Pair Notation</a>
</td>
<td> </td>
<td align="left" valign="top">A general syntax for cons cells. </td>
</tr> <tr>
<td align="left" valign="top">• <a href="association-list-type" accesskey="3">Association List Type</a>
</td>
<td> </td>
<td align="left" valign="top">A specially constructed list. </td>
</tr> </table><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-Cell-Type.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Cons-Cell-Type.html</a>
</p>
</div>
|