Common Lisp the Language, 2nd Edition


next up previous contents index
Next: Lists Up: Lists Previous: Lists


15.1. Conses

These are the basic operations on conses viewed as pairs rather than as the constituents of a list.

[Function]
carlist

This returns the car of list, which must be a cons or (); that is, list must satisfy the predicate listp. By definition, the car of () is (). If the cons is regarded as the first cons of a list, then car returns the first element of the list. For example:

(car '(a b c)) => a

See first. The car of a cons may be altered by using rplaca or setf.

[Function]
cdrlist

This returns the cdr of list, which must be a cons or (); that is, list must satisfy the predicate listp. By definition, the cdr of () is (). If the cons is regarded as the first cons of a list, then cdr returns the rest of the list, which is a list with all elements but the first of the original list. For example:

(cdr '(a b c)) => (b c)

See rest. The cdr of a cons may be altered by using rplacd or setf.

[Function]
caarlist
cadrlist
cdarlist
cddrlist
caaarlist
caadrlist
cadarlist
caddrlist
cdaarlist
cdadrlist
cddarlist
cdddrlist
caaaarlist
caaadrlist
caadarlist
caaddrlist
cadaarlist
cadadrlist
caddarlist
cadddrlist
cdaaarlist
cdaadrlist
cdadarlist
cdaddrlist
cddaarlist
cddadrlist
cdddarlist
cddddrlist

All of the compositions of up to four car and cdr operations are defined as separate Common Lisp functions. The names of these functions begin with c and end with r, and in between is a sequence of a and d letters corresponding to the composition performed by the function. For example:

(cddadr x) is the same as (cdr (cdr (car (cdr x))))

If the argument is regarded as a list, then cadr returns the second element of the list, caddr the third, and cadddr the fourth. If the first element of a list is a list, then caar is the first element of the sublist, cdar is the rest of that sublist, and cadar is the second element of the sublist, and so on.

As a matter of style, it is often preferable to define a function or macro to access part of a complicated data structure, rather than to use a long car/cdr string. For example, one might define a macro to extract the list of parameter variables from a lambda-expression:

(defmacro lambda-vars (lambda-exp) `(cadr ,lambda-exp))

and then use lambda-vars for this purpose instead of cadr. See also defstruct, which will automatically define new record data types and access functions for instances of them.

Any of these functions may be used to specify a place for setf.

[Function]
consxy

cons is the primitive function to create a new cons whose car is x and whose cdr is y. For example:

(cons 'a 'b) => (a . b) 
(cons 'a (cons 'b (cons 'c '()))) => (a b c) 
(cons 'a '(b c d)) => (a b c d)

cons may be thought of as creating a cons, or as adding a new element to the front of a list.

[Function]
tree-equalxy&key :test :test-not

This is a predicate that is true if x and y are isomorphic trees with identical leaves, that is, if x and y are atoms that satisfy the test (by default eql), or if they are both conses and their car’s are tree-equal and their cdr’s are tree-equal. Thus tree-equal recursively compares conses (but not any other objects that have components). See equal, which does recursively compare certain other structured objects, such as strings.

change_begin
X3J13 voted in January 1989 (MAPPING-DESTRUCTIVE-INTERACTION)   to restrict user side effects; see section 7.9.
change_end


next up previous contents index
Next: Lists Up: Lists Previous: Lists



AI.Repository@cs.cmu.edu