User Tools


Accessors CAR, CDR and their compositions

Syntax

  • car xobject
  • cdr xobject
  • (setf (car x) new-object)
  • (setf (cdr x) new-object)

Click here for the syntax and equivalence chart of all car and cdr compositions.

Arguments and values

Description

If x is a cons, car returns the car of that cons. If x is nil, car returns nil.

If x is a cons, cdr returns the cdr of that cons. If x is nil, cdr returns nil.

Functions are provided which perform compositions up to four car and cdr operations. Their names consist of a C, followed by two, three or four occurrences of A or D, and finally a R. The series of As and Ds in each function's name is chosen to identify the series of car and cdr operations that is performed by the function. The order in which the As and Ds appear is the inverse of the order in which the corresponding operations are performed.

setf can also be used with any of these functions to change an existing component of x, but setf will not make new components. So, for example, the car of a cons can be assigned with setf of car, but the car of nil cannot be assigned with setf of car.

Similarly, the car of the car of a cons whose car is a cons can be assigned with setf of caar, but neither nil nor a cons whose car is nil can be assigned with setf of caar. The argument x is permitted to be a dotted list or a circular list.

Examples

(car nil)

NIL

(cdr '(1 . 2))

2

(cdr '(1 2))

(2)

(cadr '(1 2))

2

(car '(a b c))

A

(cdr '(a b c))

(B C)

Exceptional situations

The functions car and cdr should signal type-error if they receive an argument which is not a list. The other functions (caar, cadr, ..., cddddr) should behave for the purpose of error checking as if defined by appropriate calls to car and cdr.

See also

Example Implementation

To be done.

Notes

The car of a cons can also be altered by using rplaca, and the cdr of a cons can be altered by using rplacd.

  • (car x) is equivalent to (first x).
  • (cadr x) is equivalent to (second x).
  • (caddr x) is equivalent to (third x).
  • (cadddr x) is equivalent to (fourth x).