Go to the previous, next section.
Dotted pair notation is an alternative syntax for cons cells
that represents the CAR and CDR explicitly. In this syntax,
(a . b) stands for a cons cell whose CAR is
the object a, and whose CDR is the object b. Dotted
pair notation is therefore more general than list syntax. In the dotted
pair notation, the list `(1 2 3)' is written as `(1 . (2 . (3
. nil)))'. For nil-terminated lists, the two notations produce
the same result, but list notation is usually clearer and more
convenient when it is applicable. When printing a list, the dotted pair
notation is only used if the CDR of a cell is not a list.
Box notation can also be used to illustrate what dotted pairs look
like. For example, (rose . violet) is diagrammed as follows:
___ ___
|___|___|--> violet
|
|
--> rose
Dotted pair notation can be combined with list notation to represent a
chain of cons cells with a non-nil final CDR. For example,
(rose violet . buttercup) is equivalent to (rose . (violet
. buttercup)). The object looks like this:
___ ___ ___ ___
|___|___|--> |___|___|--> buttercup
| |
| |
--> rose --> violet
These diagrams make it evident that (rose . violet .
buttercup) must have an invalid syntax since it would require that a
cons cell have three parts rather than two.
The list (rose violet) is equivalent to (rose . (violet))
and looks like this:
___ ___ ___ ___
|___|___|--> |___|___|--> nil
| |
| |
--> rose --> violet
Similarly, the three-element list (rose violet buttercup)
is equivalent to (rose . (violet . (buttercup))).
Go to the previous, next section.