An association list (or alist) is a list mapping key values to to other values. Each element of the alist is a cons cell, the car of which is the key, the cdr is the value that it associates to. For example an alist could look like,
((fred . 20) (bill . 30))
this alist has two keys, fred and bill which both associate
to an integer (20 and 30 respectively).
It is possible to make the associated values lists, this looks like,
((fred 20 male) (bill 30 male) (sue 25 female))
in this alist the symbol fred is associated with the list
(20 male).
There are a number of functions which let you interrogate an alist with a given key for its association.
equal to key, this element is then returned. If
no match of key is found nil is returned.
(assoc 'two '((one . 1) (two . 2) (three . 3)))
=> (two . 2)
assoc except that the function eq is
used to compare elements instead of equal.
It is not usually wise to use assq when the keys of the alist may not
be symbols -- eq won't think two objects are equivalent unless they
are the same object!
(assq "foo" '(("bar" . 1) ("foo" . 2)))
=> nil
(assoc "foo" '(("bar" . 1) ("foo" . 2)))
=> ("foo" . 2)
equal to association, that element is then returned.
nil will be returned if no elements match.
(rassoc 2 '((one . 1) (two . 2) (three . 3)))
=> (two . 2)
rassoc except that it uses eq
to make comparisons.
Go to the first, previous, next, last section, table of contents.