Association lists

Course links

Exercise 1

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

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-professors-directory for an example of the form that your definition of sidekicks should take.

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

===========================================
| Protagonist       | Sidekick            |
===========================================
| Mr. Peabody       | Sherman             |
|-----------------------------------------|
| Yogi Bear         | Boo Boo             |
|-----------------------------------------|
| Secret Squirrel   | Morocco Mole        |
|-----------------------------------------|
| Tennessee Tuxedo  | Chumley             |
|-----------------------------------------|
| Quick Draw McGraw | Baba Looey          |
|-----------------------------------------|
| Dick Dastardly    | Muttley             |
|-----------------------------------------|
| Bullwinkle        | Rocky               |
|-----------------------------------------|
| Bart Simpson      | Milhouse Van Houten |
|-----------------------------------------|
| Asterix           | Obelix              |
===========================================

Exercise 2

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 3

Redefine sidekicks so that it includes two sidekicks to the same protagonist -- say ("Batman" . "Robin") and ("Batman" . "Batgirl") are both entries. What happens if you try to apply assoc to retrieve these entries, using the common key "Batman"?

If you find the results disappointing, define and test a procedure similar to assoc, except that it returns a list of all the pairs with the given key.

Exercise 4

What happens if you search by sidekick instead of by protagonist? (For example, you might try (assoc "Chumley" sidekicks).)

If you find the results disappointing, define and test a procedure reverse-lookup that takes two arguments, an association list alist and an associated datum val, and returns a pair from alist that has val as its second component, or #f if there is no such pair.

Exercise 5

Define and test a procedure that takes two arguments, the first a positive integer sought and the second an association list als in which all of the keys are natural numbers, and returns the first pair in als in which the car is evenly divisible by sought.

(In other words, this procedure works like assoc, except that you can recover a pair from the association list if you know any divisor of its key, without having to know the key itself.)

The original author of this lab is Professor Henry Walker. Professor Ben Gum contributed the cartoon-sidekicks theme and constructed the database.