People new to Lisp quickly encounter the functions car
and cdr
which have seemingly nonsensical names. They originate in the machine
instructions of computers made in the 1950s. In computer terms that is
archaic. The names also do not describe what the functions actually do.
Many new Lispers then wonder whether car
and cdr
are still used,
given that Common Lisp is often punted as a modern language.
The answer is: Yes, Common Lisp (and some other dialects) still use car
and
cdr
. They have equivalents named first
and rest
which perform the same functions.
The elements of a cons cell are called car
and cdr
. Lists are
stored using cons cells and since they are an integral part of Lisp’s design,
the archaic names are here to stay.
Some CL programmers use car
/cdr
and first
/rest
to
distinguish between conceptually different data sets. One possibility is to use
car
/cdr
to access non-linear sequences such as trees and graphs and
to use first
/rest
to access linear sequences such as lists. This is
only a personal style which may make the code easier to follow but mixing the
functions will still give the same results.