Fundamentals of Computer Science I (CSC-151.02 2000F)


Association Lists

Consider the organization of a simple telephone directory for on-campus telephones: a sequence of entries, each consisting of a name and a four-digit telephone number. In Scheme, it's natural to use strings for names; it turns out that telephone numbers should also be represented as strings, since string operations make a useful kind of sense when applied to telephone numbers and integer operations do not. (For instance, (string-append "269-" extension) does something useful if the value of extension is a string, but not if it is an integer.)

To represent each individual entry in a telephone directory, we can use a pair, such as ("Henry Walker" . "4208"), ("John Stone" . "3181"), or ("Sam Rebelsky" . "4410"), with the name as the car of the entry and the telephone number as the cdr. An entire directory, then, would be a list of such entries:

(define science-chairs-directory
  (list (cons "Bruce Voyles" "3038")
        (cons "Diane Robertson" "3039")
        (cons "Martin Minelli" "3007")
        (cons "Emily Moore" "4201")
        (cons "Paul Tjossem" "1234")
        (cons "David Lopatto" "3168")))

In Scheme, a list of pairs is called an association list or alist.

As the telephone-directory example illustrates, a particularly common application of association lists involves looking for a desired name or first component of a pair and retrieving the second component of a pair. Thus, the first component of each pair (the car of a pair) often is called a key, and the cdr of the pair is its associated data or value. For example, in the above illustration, "Martin Minelli", "Emily Moore", and "David Lopatto" are some of the keys, and the telephone numbers are the associated data. Thus an association list is a simple way to implement a small database.

Since such applications are very common, Scheme provides procedures to retrieve from an association list the pair containing a specified key. The most frequently used procedure of this kind is assoc. Given a key and association list, assoc returns the first pair with the given key. If the key does not occur in the association list, then assoc returns #f. For example, the value of (assoc "Mark Schneider" science-chairs-directory) is ("Mark Schneider" . "3018"), while the value of (assoc "Laurel Smith" science-chairs-directory) is #f.

To find the telephone number corresponding to a given name, we could apply the cdr procedure to the result of assoc:

(define look-up-telephone-number
  (lambda (name)
    (if (assoc name science-chairs-directory)
        (cdr (assoc name science-chairs-directory))
        'unlisted)))

The value of the call (look-up-telephone-number "Paul Tjossem") is "3018" and the value of (look-up-telephone-number "Laurel Smith") is the symbol unlisted.

The assoc procedure is actually one of three related built-in procedures in Scheme; the other two are assq and assv. Each of these procedures scan association lists for keys. They differ only in the test used for determining when a key is found:

You may wish to refresh your memory on the purpose of these predicates (hint hint).

History

February 11, 2000 (Henry Walker and John Stone)

March 17, 2000 (John Stone)

Tuesday, 18 September 2000 (Sam Rebelsky)


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2000F/Readings/alist.html

Source text last modified Tue Sep 19 10:45:28 2000.

This page generated on Tue Sep 19 10:46:32 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu