CS275

Lab 02

Lists and Recursion

Due: Monday, September 24 at 11:59 PM

The goals for this assignment are increasing your skills with

You can use the solution to any exercise as a helper function in subsequent exercises, and you can also write stand--alone helper functions..

 

Exercises

Part 1 - Lists as structures

  1. Write a function merge that merges two sorted lists of numbers onto one sorted list, the way MergeSort works.
    • (merge '(1 4 5) '(2 3 4 6)) returns (1 2 3 4 4 5 6)

  2. Write a sort function for lists of numbers. Don't get the idea from (1) that you should do MergeSort. Try InsertionSort.
    • (sort '(5 1 8 3 7)) returns (1 3 5 7 8)

  3. Write function contains-sublist 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

  4. Write function rember-sublist 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)

Part 2 - Association Lists

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, representing the data for one individual. For example, we might make a phone-book as a list of name, phone number pairs. Such a structure is called an association list. For example,

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

  1. Write function (phone-number person phone-book) that returns the phone number of the given person, or the atom 'disconnected if there is no entry for the person.
    With the phone book defined above (phone-number 'nick phone-book) returns 775-0912.

  2. Write funtion (person phone-number phone-book)that returns the name of the person with the given phone number.
    So (person '775-0912 phone-book) returns nick.

Part 3 - Accumulator-Passing Style

  1. Write function (length lat) that returns the length (number of elements) of lat, and does it by calling an accumulator-passing style function (length-a lat acc). So (length '(a b c)) returns 3. Your length-a function should be tail-recursive.

  2. Write function (count a lat) that returns the number of occurrences of atom a in lat, and does it by calling an accumulator-passing style function (count-a a lat acc). So (count 'x '(a b x d c e x x a x)) returns 4.
    Your count-a function should be tail-recursive.

  3. Write function (max vec) which returns the largest element in a flat list of positive numbers.. So (max '(3 6 1 9 4 7 2 6) should return 9. Function max should call an accumulator-passing style function max-a.

  4. Function (index a lat) returns the 0-based index of the first occurrence of atom a in a flat list of atoms.. As usual, (index a l;at) should return -1 if a is not an element of lat. So (index 'c '(a b c d e d c b a) returns 2 and index( 'c '(a b d e f)) returns -11. Write function index in accumulator-passing style.