Node:car & cdr, Next:cons, Previous:Strange Names, Up:car cdr & cons
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
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
Clearly, a more reasonable name for the
car function would be
first and this is often suggested.
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,
`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
daisy buttercup) is
rose, the rest of the list, the value
returned by the
cdr function, is
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.
cdr does not remove any elements from the
list--it just returns a report of what the second and subsequent
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.
Clearly, a more reasonable name for
cdr would be
(There is a lesson here: when you name new functions, consider very carefully what you are doing, since you may be stuck with the names for far longer than you expect. The reason this document perpetuates these names is that the Emacs Lisp source code uses them, and if I did not use them, you would have a hard time reading the code; but do, please, try to avoid using these terms yourself. The people who come after you will be grateful to you.)
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
any parentheses around it.
pine is the first element in the
list. However, the CDR of the list is a list itself,
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
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
(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
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
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.)