Go to the first, previous, next, last section, table of contents.


Cons Cells

A cons cell is an ordered pair of two objects, the car and the cdr.

The read syntax of a cons cell is an opening parenthesis followed by the read syntax of the car, a dot, the read syntax of the cdr and a closing parenthesis. For example a cons cell with a car of 10 and a cdr of the string `foo' would be written as,

(10 . "foo")

Function: cons car cdr
This function creates a new cons cell. It will have a car of car and a cdr of cdr.

(cons 10 "foo")
    => (10 . "foo")

Function: consp object
This function returns t if object is a cons cell and nil otherwise.

(consp '(1 . 2))
    => t

(consp nil)
    => nil

(consp (cons 1 2))
    => t

In Lisp an atom is any object which is not a cons cell (and is, therefore, atomic).

Function: atom object
Returns t if object is an atom (not a cons cell).

Given a cons cell there are a number of operations which can be performed on it.

Function: car cons-cell
This function returns the object which the car of the cons cell cons-cell.

(car (cons 1 2))
    => 1

(car '(1 . 2))
    => 1

Function: cdr cons-cell
This function returns the cdr of the cons cell cons-cell.

(cdr (cons 1 2))
    => 2

(cdr '(1 . 2))
    => 2

Function: rplaca cons-cell new-car
This function sets the value of the car in the cons cell cons-cell to new-car. The value returned is new-car.

(setq x (cons 1 2))
    => (1 . 2)
(rplaca x 3)
    => 3
x
    => (3 . 2)

Function: rplacd cons-cell new-cdr
This function is similar to rplacd except that the cdr slot of cons-cell is modified.


Go to the first, previous, next, last section, table of contents.