Laboratory 33: Association Lists

Summary: In today's laboratory, you will experiment with association lists, structures that make it easy to look up information.

Contents:



Exercises

Exercise 1: Science Chairs, Revisited

In the reading on association lists, we claimed that the lookup-telephone-number procedure worked correctly, even for the extended table that included not only phone number, but also department.

Verify that claim.

You can find the table and the code in the reading.

Exercise 2: Science Chairs' Departments

Document and write a procedure, (lookup-department name directory), that someone who knows the name of the chair of a department can use to find that department's name.

Exercise 3: Cartoon Sidekicks

Define an association list, sidekicks, that associates the names of some famous cartoon protagonists (as strings) with the names of their sidekicks (again, as strings).

You may also want to look at the note on this exercise.

Here's a table containing information for your association list:

Protagonist Sidekick
Peabody Sherman
Yogi Booboo
Bullwinkle Rocky
Bart Simpson Milhouse Van Houten
Asterix Obelix
Strong Bad The Cheat

Exercise 4: Finding Sidekicks

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

Exercise 5: Duplicate Keys

a. Redefine sidekicks so that it includes two entries with the same protagonist and different sidekicks -- say Scooby Doo, who has both Shaggy and Scrappy Doo as sidekicks. What do you expect to happen if you try to apply assoc to retrieve these entries, using the common key "Scooby Doo"?

b. Check your answer experimentally.

c. Many people find these results disappointing. To help alleviate this disappointment, define and test a procedure, (multi-assoc key alist), similar to assoc, except that it returns a list of all the lists with the given key.

Exercise 6: Preconditions

a. What do you think that assoc will do if it is given a list in which each element is a pair, rather than a list? For example, can we use assoc to search the following list to determine the first name of a faculty member whose last name you know?

(define some-faculty
(list (cons "Walker" "Henry")
(cons "Stone" "John")
(cons "Rebelsky" "Sam")
(cons "Davis" "Janet")
(cons "Coahran" "Marge")
(cons "Adelberg" "Arnie")
(cons "Herman" "Gene")
(cons "Jepsen" "Chuck")
(cons "Moore" "Emily or Tom")
(cons "Wolf" "Royce")
(cons "Chamberland" "Marc")
(cons "Shuman" "Karen")
(cons "French" "Chris")
(cons "Kornelson" "Keri")
(cons "Romano" "David")
(cons "Mosley" "Holly")
(cons "Kuiper" "Shonda")
(cons "Jager" "Leah")))

b. Confirm or refute your answers by experimentation.

c. Based on your experience, what preconditions should assoc have?

Exercise 7: Reverse Associations

a. What happens if you search the hero/sidekick list by sidekick instead of by protagonist? For example, you might try

(assoc "Booboo" sidekicks)

b. Define and test a procedure lookup-by-second that takes two arguments, an association list, alist, and an associated datum val, and returns

In solving this problem, you may not use the generic lookup procedure from the reading.

c. Define and test a procedure that takes two parameters, an association list, alist, and an associated datum, val, and returns a list of all elements that have val as the second component.

Exercise 8: Using a Specific Database

For some problems, it seems natural to always use a specific database, rather than to pass the database as a parameter. For example, suppose we'd set up a table of science department chairs (which may sound familliar from the reading, although we've expressed it differently here).

;;; Value:
;;; science-department-chairs
;;; Type:
;;; List of lists.
;;; Each sublist is of length two and contains a department (or "science")
;;; and a name.
;;; Both of those values are strings.
;;; Contents:
;;; A list of the department and division chairs in the Science division
;;; in academic year 2006-2007.
(define science-chairs-directory
(list (list "Samuel A. Rebelsky" "4410" "Science")
(list "Vince Eckhart" "4354" "Biology")
(list "Lee Sharpe" "3008" "Chemistry")
(list "Henry Walker" "4208" "Computer Science")
(list "Henry Walker" "4208" "CS") ; Yay shorthand!
(list "Marc Chamberland" "4207" "Mathematics and Statistics")
(list "Marc Chamberland" "4207" "Mathematics") ; More common
(list "Marc Chamberland" "4207" "Statistics") ; Less common
(list "Bob Cadmus" "3016" "Physics")
(list "Janet Gibson" "3168" "Psychology")))

We can write a procedure to look up a department chair as follows:

;;; Procedure:
;;; lookup-science-chair
;;; Parameters:
;;; dept, the name of a science department (or simply "Science")
;;; Purpose:
;;; Look up the chair of a science department.
;;; Produces:
;;; chair, a string (or #f)
;;; Preconditions:
;;; science-department-chairs must be defined appropriately
;;; dept must be a string
;;; Postconditions:
;;; If science-department-chairs specifies a chair for dept,
;;; chair is that chair.
;;; Otherwise, chair is false (#f)
(define lookup-science-chair
(lambda (dept)
(assoc dept science-department-chairs)))

The strategy of using a specific database in a procedure is often called hard-coding the database.

a. Using lookup-science-chair, look up the chair of this department.

b. Using lookup-science-chair, look up the chair of Geology.

c. Suppose we wanted to write the converse procedure (one that given a name, tells which department he or she chairs). Can we still hard-code the database? If so, show how. If not, explain why not.

For Those with Extra Time

Extra 1: Compound Keys

Consider the following list of faculty in Mathematics, Computer Science, and Statistics.

(define csms
(list
(list "Sam" "Rebelsky" "Associate" "Computer Science")
(list "Henry" "Walker" "Chair" "Computer Science")
(list "John" "Stone" "Instructor" "Computer Science")
(list "Janet" "Davis" "Assistant" "Computer Science")
(list "Marge" "Coahran" "Visitor" "Computer Science")
(list "Emily" "Moore" "Full" "Mathematics")
(list "Karen" "Shuman" "Assistant" "Mathematics")
(list "Keri" "Kornelson" "Assistant" "Mathematics")
(list "Holly" "Mosley" "Visitor" "Mathematics")
(list "David" "Romano" "Assistant" "Mathematics")
(list "Marc" "Chamberland" "Chair" "Mathematics")
(list "Royce" "Wolf" "Associate" "Mathematics")
(list "Gene" "Herman" "SFS" "Mathematics")
(list "Chuck" "Jepsen" "SFS" "Mathematics")
(list "Arnie" "Adelberg" "SFS" "Mathematics")
(list "Tom" "Moore" "Full" "Statistics")
(list "Shonda" "Kuiper" "Assistant" "Statistics")
(list "Leah" "Jager" "Visitor" "Statistics")))

When looking up people, we might want to make sure that we match both first and last names. Write a procedure, (lookup first last people) that extracts the entry in which both first and last match.

Notes

Here you will find notes on selected problems.

Notes on Exercise 3

Note: The value of sidekicks is not a procedure, so it is not necessary to use a lambda-expression in this exercise. Look at the definition of science-chairs-directory for an example of the form that your definition of sidekicks should take.

Return to the exercise.


Janet Davis (davisjan@cs.grinnell.edu)

Created April 14, 2007 based on  http://www.cs.grinnell.edu/~davisjan/csc/151/2006F/labs/30.association-lists.html
Last revised April 14,
2007
With thanks to Sam Rebelsky