Common Lisp/Reference/cons

The cons is both Lisp's fundamental memory object and the name of a Lisp operator. The cons, as the memory object, is a memory construct which is able to store a pair of objects, known as the car and the cdr.

The cons operator offers a way to both create a cons object and also set the values. So, for example, the following command creates a cons object with the car field set to 1 and the cdr field set to 2:

> (cons 1 2)

(1 . 2)

As the cons object can also store references to other cons objects in its car and cdr fields, the cons field is also able to be used as a basic building block for a series of data structures. So, a singly linked list could be constructed with cons objects such as:

> (cons 1 (cons 2 (cons 3 nil)))

(1 2 3)

The previous car nested structure generates an equivalent singly linked list to the following:

> (list 1 2 3)

(1 2 3)


See also

edit