CS275

HW 02

Flat Lists and Association Lists

Due: Monday, February 17

The goals for this assignment are increasing your skills with

For this assignment you will work together in groups of 2 or 3. You must work in a group, even if you don't like it. Each group will produce one solution file. Put the following comment at the top of the file:

; Lab 2 
names of the members of your group 
Save the file as "hw2.ss".

Your group should solve each of the exercises in the following way. One person will act as "scribe" for the problem. The scribe's job is to enter the code into the computer, along with several test examples. The scribe role should rotate around the group so that everyone has a chance to do it an equal number of times. All members of the group should participate in writing each solution, and deciding on test cases. Try to ensure that your test cases are comprehensive; don't use only the test cases provided to check your code.

You can use the solution to any exercise as a helper function in subsequent exercises.

Assume the following data types for input to the functions:

a, b, c ... atoms (numbers or symbols)
n, m, ... numbers
lat A list of atoms
lyst A list of any kind
exp Any expression (atom or list)

Exercises

Part 1 - Lists as structures

  1. Write a function that determines if a list contains a particular sublist:.
    • (contains-sublist  '(2 3 4)  '(1 2 3 4 5) ) returns #t
    • (contains-sublist  '(2 3 4)  '(1 2 5 3 4)) returns #f
  1. Write a function that removes the first occurrance of the sublist from the given list
    • (rember-sublist  '(2 3 4)  '(1 2 3 4 5 ) ) returns (1 5)

  2. Write a function that merges two sorted lists onto one sorted list, a la MergeSort :
    • (merge '(1 4 5) '(2 3 4 6)) returns (1 2 3 4 4 5 6)

  3. Write a function (make-time hours minutes seconds) that stores the hours, minutes and seconds as a triple. Write function (increment-time t) that adds one second to such a time t. Finally, write (compare-time t1 t2) that returns -1 if t1 < t2, 0 if t1 = t2, and 1 if t1 > t2.

Part 2 - Flat Lists as data structures

We could store a very small phone book as

(define phone-book
          '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043))

This phone book is a flat list of atoms. For each person represented in the book there are two entries -- first the person's name, then the person's phone number. In the following exercises we will pass an argument phone-book that has this structure.

  1. Write function (phone-number person phone-book) that returns the number corresponding to the person, if there is one, or the atom 'disconnected if there is no number for this person. So (phone-number 'luke '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043) ) will return '774-22829. Be careful how you do this -- (phone-number '774-2839   '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043) ) should return 'disconnected, not a name. As you recurse looking for a match for the person you should only check every-other entry. You can assume that the phone-book argument is properly structured.

  2. Now write a person function that looks up phone numbers and returns the owner of the number.
    (person '774-2839 '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043) ) should return 'luke.

Part 3 - Association Lists

As you surely noticed in Part 2, flat lists aren't a very good way to store data. An obvious improvement is to have the list consist of elements, each of which is itself a list whose first element is a person's name and whose remaining elements are the data items we want to associate with that person. For example,

(define phone-book
         '( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) ))

Such as structure is called an association list.

  1. Write function (AL-phone-number person AL-phone-book) that returns the phone number of the given person, or the atom 'disconnected if there is no entry for the person.

  2. Write funtion (AL-person phone-number AL-phone-book) that returns the name of the person with the given phone number.

    So (AL-phone-number 'luke  '( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) )) returns '774-2839 and
    (AL-person '774-2839  '( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) )) returns 'luke.

  3. Write function old->new that takes a phone book in the style of Part 2 and returns an association list phone book.

    So (old->new '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043)) returns  
                            '( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) )

  4. Write function new->old that takes an association list phone book and returns a flat list phone book:

    (new->old  '( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) )) returns
                            '(barbara 775-1234 luke 774-2839 nick 775-0912 valerie 775-9043))

  5.  

Part 4 - Vital Statistics

We could put any data elements into an association list. As a simple example we will expand our structure so that each entry of the assocation list is a triple (name, phone-number, birthday), as in '(luke 774-2839 6-12-81) . To keep our code clean and understandable we will make use of some helper functions, such as (define name (lambda (entry) (car entry))).

  1. Write helper function number that takes such an triple and returns its middle element as the phone number: (number '(luke 774-2839 6-12-81)) is '774-2839.

  2. Write helper function bday that takes such a triple and returns its third element as the birthday: (number '(luke 774-2839 6-12-81)) is '6-12-81.

  3. Use these helper functions to write function birthday that takes an association list with this structure and returns the birthday of the given person. For example,

    (birthday 'barbara '((barbara 775-1234 1-2-74) (luke 774-2839 6-12-81))) returns '1-2-74

    We could easily write functions that search the association list for other pieces of data.