car
and cdr
The CAR of a list is, quite simply, the first item in the list.
Thus the CAR of the list (rose violet daisy buttercup)
is
rose
.
If you are reading this in Info in GNU Emacs, you can see this by evaluating the following:
(car '(rose violet daisy buttercup))
After evaluating the expression, rose
will appear in the echo
area.
car
does not remove the first item from the list; it only reports
what it is. After car
has been applied to a list, the list is
still the same as it was. In the jargon, car
is
“non-destructive”. This feature turns out to be important.
The CDR of a list is the rest of the list, that is, the
cdr
function returns the part of the list that follows the
first item. Thus, while the CAR of the list '(rose violet
daisy buttercup)
is rose
, the rest of the list, the value
returned by the cdr
function, is (violet daisy
buttercup)
.
You can see this by evaluating the following in the usual way:
(cdr '(rose violet daisy buttercup))
When you evaluate this, (violet daisy buttercup)
will appear in
the echo area.
Like car
, cdr
does not remove any elements from the
list—it just returns a report of what the second and subsequent
elements are.
Incidentally, in the example, the list of flowers is quoted. If it were
not, the Lisp interpreter would try to evaluate the list by calling
rose
as a function. In this example, we do not want to do that.
For operating on lists, the names first
and rest
would
make more sense than the names car
and cdr
. Indeed,
some programmers define first
and rest
as aliases for
car
and cdr
, then write first
and rest
in
their code.
However, lists in Lisp are built using a lower-level structure known
as “cons cells” (see How Lists are Implemented), in which there is no
such thing as “first” or “rest”, and the CAR and the CDR
are symmetrical. Lisp does not try to hide the existence of cons
cells, and programs do use them for things other than lists. For this
reason, the names are helpful for reminding programmers that
car
and cdr
are in fact symmetrical, despite the
asymmetrical way they are used in lists.
When car
and cdr
are applied to a list made up of symbols,
such as the list (pine fir oak maple)
, the element of the list
returned by the function car
is the symbol pine
without
any parentheses around it. pine
is the first element in the
list. However, the CDR of the list is a list itself, (fir
oak maple)
, as you can see by evaluating the following expressions in
the usual way:
(car '(pine fir oak maple)) (cdr '(pine fir oak maple))
On the other hand, in a list of lists, the first element is itself a
list. car
returns this first element as a list. For example,
the following list contains three sub-lists, a list of carnivores, a
list of herbivores and a list of sea mammals:
(car '((lion tiger cheetah) (gazelle antelope zebra) (whale dolphin seal)))
In this example, the first element or CAR of the list is the list of
carnivores, (lion tiger cheetah)
, and the rest of the list is
((gazelle antelope zebra) (whale dolphin seal))
.
(cdr '((lion tiger cheetah) (gazelle antelope zebra) (whale dolphin seal)))
It is worth saying again that car
and cdr
are
non-destructive—that is, they do not modify or change lists to which
they are applied. This is very important for how they are used.
Also, in the first chapter, in the discussion about atoms, I said that
in Lisp, certain kinds of atom, such as an array, can be separated
into parts; but the mechanism for doing this is different from the
mechanism for splitting a list. As far as Lisp is concerned, the
atoms of a list are unsplittable. (See Lisp Atoms.) The
car
and cdr
functions are used for splitting lists and
are considered fundamental to Lisp. Since they cannot split or gain
access to the parts of an array, an array is considered an atom.
Conversely, the other fundamental function, cons
, can put
together or construct a list, but not an array. (Arrays are handled
by array-specific functions. See Arrays in The GNU
Emacs Lisp Reference Manual.)