Next: Association List Type, Previous: Cons Cell Type, Up: Cons Cell Type [Contents][Index]
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.
Here’s how box notation can illustrate dotted pairs. This example
shows the pair (rose . violet)
:
___ ___ |___|___|--> 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 why (rose . violet . buttercup)
is invalid syntax; it would require a cons cell that has
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)))
.
Next: Association List Type, Previous: Cons Cell Type, Up: Cons Cell Type [Contents][Index]