Vital statistics

Chapter: Vital statistics

We might want more information than just the phone number in the phone book. We could store birthdays as well, for example:

 
(define phone-and-birthday-book
  '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81) 
    (nick 775-0912 3-5-22) (valerie 775-9043 9-1-71)))

Q. 19
Where is Nick's birthday in the phone-and-birthday-book?


Q. 20
Why didn't we just write (caddaddr phone-and-birthday-book)?


Q. 21
Where is Valerie's birthday in the phone-and-birthday-book?


Whenever we want the phone number stored in an entry, we will take the cadr of the entry. Whenever we want the birthday stored in an entry, we will take the caddr of the entry. It is more convenient and comprehensible to define functions that extract these elements of an entry rather than using chains of car's and cdr's:

(define number (lambda (entry) (cadr entry)))
(define birthday (lambda (entry) (caddr entry)))

Now Nick's phone number is:

(number (caddr phone-and-birthday-book))

Q. 22
Write the phone-number function using the number function.


Q. 23
We used the function number to extract the phone number. What about the name?


Q. 24
Now rewrite phone-number using name as well.


Q. 25
When we use name or number, the argument is always (car ...). Wouldn't the following be better?

 
(define name (lambda (entry) (car (car entry))))
(define number (lambda (entry) (cadr (car entry))))
(define birthday (lambda (entry) (caddr (car entry))))

(define phone-number
  (lambda (person phone-and-birthday-book)
    (cond [(null? phone-and-birthday-book) 'disconnected]
	  [(eq? person (name phone-and-birthday-book))
	   (number phone-and-birthday-book)]
	  [else (phone-number person (cdr phone-and-birthday-book))])))



Exercise 8

Use the definitions:

 
(define name (lambda (entry) (car entry)))
(define number (lambda (entry) (cadr entry)))
(define birthday (lambda (entry) (caddr entry)))

To write the birth-date function, which returns someone's birthday.

Examples:

(birth-date 'luke '()) => dont-know
(birth-date 'barbara '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81))) => 1-2-74



Exercise 9

The birth-date function is pretty similar to the phone-number function. Write the function vital-statistics that can return any element of someone's entry.

Examples:

(vital-statistics number 'barbara
          '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81))) => 775-1234

(vital-statistics birthday 'barbara
          '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81))) => 1-2-74

(vital-statistics name 'barbara
          '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81))) => barbara



Exercise 10

If we have several people with the same name in a phone book, we might want to use a person's birthday to be sure we get the number of the right one. Using name, number, and birthday, write a new phone-number function that has this behavior.

Examples:

 
(phone-number 'valerie '3-5-22 
      '((valerie 775-9043 9-1-71) (nick 775-0912 3-5-22) 
        (valerie 774-9966 3-5-22) (valerie 774-5432 12-2-67))) 
=> 774-9966

 
(phone-number 'valerie '3-5-52 
      '((valerie 775-9043 9-1-71) (nick 775-0912 3-5-22) 
        (valerie 774-9966 3-5-22) (valerie 774-5432 12-2-67))) 
=> disconnected


Exercise 11

In this era of the internet, fax machines, and cellular phones, many people have more than one phone number. Write the function phone-numbers that given the name of a person returns all of the associated phone numbers. For this exercise, you can assume that every entry with a given name has the same birthdate.

Examples:

 
(phone-numbers 'valerie 
       '((valerie 775-9043 9-1-71) (nick 775-0912 3-5-22) 
         (valerie 774-9966 9-1-71) (valerie 774-5432 9-1-71)))
=> (775-9043 774-9966 774-5432)

 
(phone-numbers 'luke 
       '((valerie 775-9043 9-1-71) (nick 775-0912 3-5-22) 
         (valerie 774-9966 9-1-71) (valerie 774-5432 9-1-71)))
=> ()

Note that the case for the empty list no longer returns disconnected!



rhypj@gwu.edu