Association lists

Chapter: Association lists

In the last chapter we represented a phone book as a list of the form:

(person phone-number person phone-number ...)

This representation caused us some problems, because some elements of the list are names and some are phone numbers. It would be easier to write functions that use the phone book if the structure were more uniform.

Thus, instead of a flat list:

(person phone-number person phone-number ...)

we use an association list:

((person phone-number) (person phone-number) ...)

Each element of an association list contains a key and the associated value. In a phone book, the key is the person and the value is the phone number.

Q. 14
Where is Barbara in the new phone book:
 
(define phone-book
  '((barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043)))


Q. 15
Where is her phone number?


Q. 16
Where is Luke?


Q. 17
Where is his phone number?


Q. 18
Write phone-number using the new representation of phone books.



Exercise 5

Write person using the new representation of phone books.


Exercise 6

Write the function old->new that converts an old phone book into a new phone book.

Example:

(old->new '(nick 775-0912 valerie 775-9043)) ((nick 775-0912) (valerie 775-9043))
(old->new '()) ()
(phone-number 'valerie (old->new '(nick 775-0912 valerie 775-9043))) 775-9043



Exercise 7

Write the function new->old that converts a new phone book into an old phone book.


rms@cs.oberlin.edu