Contents    Page-10    Prev    Next    Page+10    Index   

Association List

An association list or alist is a simple lookup table or map: a linked list containing a key value and some information associated with the key.

(assoc 'two '((one 1) (two 2) (three 3)))
    ->  (two 2)

public static Cons assoc(Object key, Cons lst) {
  if ( lst == null )
     return null;
  else if ( key.equals(first((Cons) first(lst))) )
      return ((Cons) first(lst));
          else return assoc(key, rest(lst)); }

(defun assoc (key lst)
  (if (null lst)
      (if (equal key (first (first lst)))
          (first lst)
          (assoc key (rest lst)) ) ) )

New items can be added to the association list using cons.

Adv: Simple code. Table is easily expanded.

Dis: O(n) lookup: Suitable only for small tables.