How do you recover the contents of a pair?
To get the first of the two components, use the car procedure;
to get the second, use the cdr
procedure.
The car procedure is of arity 1 and can be applied only to a
pair; giving it an operand of any other type is an error.
Why `car' and `cdr' instead of something sensible, like `left' and `right' or `fore' and `aft'?> (define pr (cons 1 2)) #> (car pr) 1 > (car (cons 66 81)) 66 > (car (cons (cons (cons 1 2) 3) 4)) ((1 . 2) . 3) > (car 54) ERROR: car: Wrong type in arg1 54
The names are historical artifacts. The first high-level language in which pairs were provided as a built-in data type, Lisp, was first implemented on a machine, the IBM 704, in which it was convenient to store a pair in two of the processor's registers. These registers were generally used for other purposes. One, the ``address register,'' usually held the address of some memory word; the other, the ``decrement register,'' usually held some offset or amount to be subtracted from another value. The name `car' is an acronym for ``contents of address register,'' and `cdr' for ``contents of decrement register.'' The names have persisted even though they are completely irrelevant to the way pairs are stored under current machine architectures.
Next topic
Previous topic
Table of contents
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/scheme-web/car.html