Pairs

Box-and-pointer diagrams

As we have seen, Scheme uses cons to build lists. We now consider a graphical way to represent the result of a cons procedure. The basic idea is to use a rectangle, divided in half, to represent the result of the cons. From the first half of the rectangle, we draw an arrow to the head of a list; from the second half of the rectangle, we draw an arrow to the rest of the list. For example, (cons 'a '()) would be represented as follows:

a divided rectangle with A on the left and the null list on the right

Here, the line to a indicates that this is the head of the list. The diagonal line through the right half of the rectangle indicates that nothing comes later in this list. Since (cons 'a '()) gives the list (a), this diagram represents (a) as well.

Now consider the list (cons 'b '(a)) or (b a). Here, we draw another rectangle, where the head points to b and the tail points to the representation of (a) that we already have seen. The result is:

a divided rectangle with B on the left and a pointer to the preceding diagram on the right

Similarly, the list (d c b a) is constructed as (cons 'd (cons 'c (cons 'b (cons 'a '())))) and would be drawn as follows:

a diagram including four divided rectangles

A similar approach may be used for lists that have other lists as elements. For example, consider the list ((a) b (c d) e). This is a list with four components, so at the top level we will need four rectangles, just as in the previous example for the list (d c b a). Here, however, the first component designates the list (a), which itself involves the box-and-pointer diagram already discussed. Similarly, the list (c d) has two boxes for its two components (just as we discussed for (b a) earlier). The resulting diagram follows:

a diagram including seven divided rectangles

Additional discussion and examples may be found in the first few pages of section 11.3 of the textbook.

Throughout these diagrams, the null list is represented by a null pointer or line. Thus, the list containing the null list, (()) -- that is, (cons '() '()) -- is represented by a rectangle with lines through both halves:

a divided rectangle with a null list in each half

  1. Draw box-and-pointer diagrams for each of the following lists:

    1. ((x) y z)
    2. (x (y z))
    3. ((a) b (c ()))

While we consistently have discussed cons in the context of lists, Scheme allows cons to be applied when the second element is not a list. For example, (cons 'a 'b) is a legal expression which can be represented by the following box-and-pointer diagram:

a divided rectangle with A on the left and B on the right

To represent such an expression on paper or as a Scheme printout, dot notation is used: (a . b) Here, the dot indicates that cons has been applied, but the second argument is not a list. Similarly,
(cons 1 'a) may be written (1 . a) and (cons "Henry" "Walker") produces
("Henry" . "Walker"). Using a box-and-pointer representation, this last result would be drawn as follows:

a divided rectangle with the string Henry on the left and the string Walker on the right

The car and cdr procedures can be used to recover the halves of one of these dotted pair structures:

(car '(a . b)) ===> a
(cdr '(a . b)) ===> b

Note that the cdr of such a structure is not a list.

  1. Enter the following into Scheme. In each case, explain when the dot notation appears or why it is not needed.

    1. (cons 'a "Walker")
    2. (cons 'a '())
    3. (cons '() 'a)
    4. (cons '() '())

  2. Draw each of the expressions in the previous step using the box-and-pointer representation.

The pair? predicate returns #t when it is given any dotted-pair structure, or indeed any structure that cons can possibly return as its value. (Basically, pair? simply determines whether the object it is given is one of those two-box rectangles.)

Association lists

Consider the organization in a simple telephone directory: a sequence of entries, each including a name and a telephone number.

One way to write such a directory in Scheme is to consider each entry as a two-element list, such as ("Walker" 4208) or ("Stone" 3181). An entire directory, then, would be a list of such entries:

(define math-cs-directory
  '(("Adelberg" 4201) ("Barnet" 4206) ("Chamberland" 4207) ("Herman" 4202)
    ("Imig" 4205) ("Jepsen" 4203) ("Jones" 4204) ("Rebelsky" 4410)
    ("Stone" 3181) ("Walker" 4208) ("Wolf" 4209)))

In Scheme, such 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 element of a pair and retrieving the second element of a pair. Thus, the first element of each pair (the car of a pair) often is called a key value, and the remained or the pair is its associated data. For example, in the above illustration, "Herman", "Stone", "Walker" are the keys, and the numbers are the associated data. As this example suggests, association lists are a simple way to implement small databases.

Since such applications are so common, Scheme provides procedures to retrieve a pair containing a desired key. The most frequently used such procedure is assoc. Given a key and association list, assoc returns the first pair with the given key. If the key does not match any key, then assoc returns false (#f). For example, (assoc "Stone" math-cs-directory) returns ("Stone" 3181), while (assoc "Smith" math-cs-directory) returns #f.

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

(define look-up-telephone-number
  (lambda (name)
    (if (assoc name math-cs-directory)
        (cadr (assoc name math-cs-directory))
        'unlisted)))

Now (look-up-telephone-number "Stone") returns 3181 and (look-up-telephone-number "Smith") returns the symbol unlisted.

  1. Define an association list birthdays which associates peoples' names (as strings) with their birthdays (again, as strings). Thus, a typical entry might be ("Lincoln" "February 12, 1809").

    Use the assoc procedure to search this association list for someone who is on the list and for someone who is not on the list.

  2. Redefine birthdays so that it includes entries for two people who have the same surname -- say, John Adams (born October 30, 1735) and John Quincy Adams (born July 11, 1767). What happens if you try to retrieve a pair with assoc using this common key?

  3. What happens if you search by date instead of by person? (For example, you might try (assoc "February 12, 1809" birthdays).) Define a Scheme procedure reverse-lookup that takes two arguments, an association list alist and an ``associated value'' val, and returns a two-element list from alist that has val as its second element.

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:

(See the earlier lab ``Lists, Booleans, and predicates'' to refresh your memory about the details of these predicates.)


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/courses/scheme/pairs.html

created February 21, 1997
last revised September 25, 1997

Henry Walker (walker@math.grin.edu) and John David Stone (stone@math.grin.edu)