Previous Next Contents

5. Conses

A cons is just a two-field record. The fields are called ``car'' and ``cdr'', for historical reasons. (On the first machine where LISP was implemented, there were two instructions CAR and CDR which stood for "Contents of Address portion of Register" and ""Contents of Decrement portion of Register". Registers were twice as wide as the width of an address, so you could have 2 adresses in one register. (sizeof(int)== 2*sizeof(void *) ) Conses were implemented using these two registers.)

Conses are easy to use:

> (cons 4 5)            ;Allocate a cons. Set the car to 4 and the cdr to 5.
(4 . 5)
                        ;you have just allocated memory and promptly
                        ;forgotten about it. In C this would cause 
                        ;a memory leak, but not in LISP.
> (cons (cons 4 5) 6)
((4 . 5) . 6)
> (car (cons 4 5))
4
> (cdr (cons 4 5))
5


Previous Next Contents