15. LISTS
This chapter contains the following sections:
15.1 Cons Functions
15.2 List Functions
15.3 Alteration of List Structure
15.4 Substitution of Expressions
15.5 Using Lists as Sets
15.6 Association Lists
A cons, or dotted pair, is a compound data object having two components called the car and cdr. Each component may be any LISP object. A list is a chain of conses linked by cdr fields; the chain is terminated by some atom (a non-cons object). An ordinary list is termined by nil, the empty list (also written ()). A list whose cdr chain is termined by some non-nil atom is called a dotted list.
The recommended predicate for testing for the end of a list is endp.
15.1 Cons Functions
These are the basic operations on conses, viewed as pairs rather than as the constituents of a list.
car
cdr
caar
cadr
cdar
cddr
caaar
caadr
cadar
caddr
cdaar
cdadr
cddar
cdddr
caaaar
caaadr
caadar
caaddr
cadaar
cadadr
caddar
cadddr
cdaaar
cdaadr
cdadar
cdaddr
cddaar
cddadr
cdddar
cddddr
cons
tree-equal
15.2 List Functions
The following functions perform various operations on lists.
endp
list-length
nth
first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth
rest
nthcdr
last
list
list*
make-list
append
copy-list
copy-alist
copy-tree
revappend
nconc
nreconc
push
pushnew
pop
butlast
nbutlast
ldiff
15.3 Alteration of List Structure
The following functions may be used to make alterations in already existing list structure, that is, to change the car or cdr of an existing cons.
rplaca
rplacd
15.4 Substitution of Expressions
A number of functions are provided for performing substitutions within a tree. All take a tree and description of old subexpressions to be replaced by new ones. They come in non-destructive and destructive varieties and specify substitution either by two arguments or by an association list.
subst
subst-if
subst-if-not
nsubst
nsubst-if
nsubst-if-not
sublis
nsublis
15.5 Using Lists as Sets
There following functions allow lists to be operated on as sets.
member
member-if
member-if-not
tailp
adjoin
union
nunion
intersection
nintersection
set-difference
nset-difference
set-exclusive-or
nset-exclusive-or
subsetp
15.6 Association Lists
An association list, or a-list, is a data structure used very frequently in LISP. An a-list is a list of pairs (conses); each pair is an association. The car of a pair is called the key, and the cdr is called the datum.
An advantage of the a-list representation is that a-list can be incrementally augmented simply by adding new entries to the front. Moreover, because the searching function assoc searches the a-list in order, new entries can shadow old entries. If an a-list is viewed as a mapping from keys to data, then the mapping can be not only augmented but also altered in a non-destructive manner by adding new entries to the front of the a-list.
Sometimes an a-list represents a bijective mapping, and it is desirable to retrieve a key given a datum. For this purpose, the reverse searching function rassoc is provided. Other variants of a-list searches can be constructed using the function find or member.
It is permissible to let nil be an element of an a-list in place of a pair. Such an element is not considered to be a pair but is simply passed over when the a-list is searched by assoc.
The following functions manipulate association lists.
acons
pairlis
assoc
assoc-if
assoc-if-not
rassoc
rassoc-if
rassoc-if-not