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
|
<h4 class="subsubsection">Dotted Pair Notation</h4> <p><em>Dotted pair notation</em> is a general syntax for cons cells that represents the <small>CAR</small> and <small>CDR</small> explicitly. In this syntax, <code>(<var>a</var> . <var>b</var>)</code> stands for a cons cell whose <small>CAR</small> is the object <var>a</var> and whose <small>CDR</small> is the object <var>b</var>. Dotted pair notation is more general than list syntax because the <small>CDR</small> does not have to be a list. However, it is more cumbersome in cases where list syntax would work. In dotted pair notation, the list ‘<samp>(1 2 3)</samp>’ is written as ‘<samp>(1 . (2 . (3 . nil)))</samp>’. For <code>nil</code>-terminated lists, you can use either notation, but list notation is usually clearer and more convenient. When printing a list, the dotted pair notation is only used if the <small>CDR</small> of a cons cell is not a list. </p> <p>Here’s an example using boxes to illustrate dotted pair notation. This example shows the pair <code>(rose . violet)</code>: </p> <div class="example"> <pre class="example"> --- ---
| | |--> violet
--- ---
|
|
--> rose
</pre>
</div> <p>You can combine dotted pair notation with list notation to represent conveniently a chain of cons cells with a non-<code>nil</code> final <small>CDR</small>. You write a dot after the last element of the list, followed by the <small>CDR</small> of the final cons cell. For example, <code>(rose violet
. buttercup)</code> is equivalent to <code>(rose . (violet . buttercup))</code>. The object looks like this: </p> <div class="example"> <pre class="example"> --- --- --- ---
| | |--> | | |--> buttercup
--- --- --- ---
| |
| |
--> rose --> violet
</pre>
</div> <p>The syntax <code>(rose . violet . buttercup)</code> is invalid because there is nothing that it could mean. If anything, it would say to put <code>buttercup</code> in the <small>CDR</small> of a cons cell whose <small>CDR</small> is already used for <code>violet</code>. </p> <p>The list <code>(rose violet)</code> is equivalent to <code>(rose . (violet))</code>, and looks like this: </p> <div class="example"> <pre class="example"> --- --- --- ---
| | |--> | | |--> nil
--- --- --- ---
| |
| |
--> rose --> violet
</pre>
</div> <p>Similarly, the three-element list <code>(rose violet buttercup)</code> is equivalent to <code>(rose . (violet . (buttercup)))</code>. It looks like this: </p> <div class="example"> <pre class="example"> --- --- --- --- --- ---
| | |--> | | |--> | | |--> nil
--- --- --- --- --- ---
| | |
| | |
--> rose --> violet --> buttercup
</pre>
</div> <p>As a somewhat peculiar side effect of <code>(a b . c)</code> and <code>(a . (b . c))</code> being equivalent, for consistency this means that if you replace <code>b</code> here with the empty sequence, then it follows that <code>(a . c)</code> and <code>(a . ( . c))</code> are equivalent, too. This also means that <code>( . c)</code> is equivalent to <code>c</code>, but this is seldom used. </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/Dotted-Pair-Notation.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Dotted-Pair-Notation.html</a>
</p>
</div>
|