file: 10-C-lists.muf package: muf status: alpha
An association list is a list of cells containing key/val pairs in their car/cdr slots:
Stack: [ key0 val0 cons key1 val1 cons key2 val2 cons ]l --> list
Association lists are frequently used in traditional Lisp programming to remember keyval associations, and typically modified nondestructively by pushing and popping new keyval cells at the start of the list.
The assoc
function takes an association list and a
key, a returns the first keyval cell containing that key, if
any, else nil
. Thus, you must take the cdr
of
the returned value to find the actual value associated with
the key.
If no matching key is found, nil
is returned.
The choice of return value may see a bit odd. One
advantage of this arrangement is that it makes it
easy to distinguish between failure to find the
key, and finding that the key has a value of nil
.
Stack: [ 'k' 'v' cons 'K' 'V' cons ]l --> list Stack: 'k' list assoc cdr Stack: 'v' pop 'K' list assoc cdr Stack: 'V'
See section rassoc.
Go to the first, previous, next, last section, table of contents.