Association Lists

A list consisting of one or more list of exactly two elements each is called an associative list, or a-list. A-lists are a common data structure used for indexing information; the first member of a pair is called the key, while the second member is the data being indexed. For example, a list of programmers and the systems they developed may go like this:

   (define OS-Developers '(("Linus Torvalds" "Linux") 
                           ("Richard Stallman" "HURD")
                           ("Bill Jolitz" "FreeBSD")
                           ("Pype-Clicker" "Clicker32")
                           ("Tim Robinson" "Mobius")
                           ("Solar" "Pro-POS")
                           ("Jay Osako" "Thelema") 
                           (" Robert Szeleney" "SkyOS")))
  

The function (assoc) takes an a-list and a key value, and returns the key-value pair (or #f if it is not found in the list).

   > (assoc "Jay Osako" OS-Developers)
   ("Jay Osako" "Thelema")

   > (assoc "HURD" OS-Developers)
   #f

    > (assoc "Richard Stallman" OS-Developers)
   ("Richard Stallman" "HURD")
  

Contents Previous Next