CS275
Flat Lists and Association Lists
Due: Monday, February 17
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 2Save the file as "hw2.ss".
; names of the members of your group
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)
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.
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.
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.
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) )
(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))
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))).
(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.