The goals for this assignment are increasing your skills with
- Writing Scheme functions
- Using recursion with flat lists
- Using lists to structure data
You can use the solution to any exercise as a helper function in subsequent exercises, and you can also write stand--alone helper functions..
Part 1 - Lists as structures
- Write a function merge that merges two sorted lists of numbers onto one sorted list,
the way MergeSort works.
Write a sort function for lists of numbers. Don't get the idea from (1) that you should do MergeSort. Try InsertionSort.
- (merge '(1 4 5) '(2 3 4 6)) returns (1 2 3 4 4 5 6)
Write function contains-sublist that determines if a list contains a particular sublist:.
- (sort '(5 1 8 3 7)) returns (1 3 5 7 8)
Write function rember-sublist that removes the first occurrance of the sublist from the given list
- (contains-sublist '(2 3 4) '(1 2 3 4 5) ) returns #t
- (contains-sublist '(2 3 4) '(1 2 5 3 4)) returns #f
- (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,
'( (barbara 775-1234) (luke 774-2839) (nick 775-0912) (valerie 775-9043) ))
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.
- 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 - Recursive Styles
- 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). Your length-a function should be tail-recursive.
So (length '(a b c)) returns 3
- 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). Your count-a function should be tail-recursive.
(count 'x '(a b x d c e x x a x)) returns 4.
- Write function (length2 lat) that returns the length of lat and does it by calling a continuation-passing style function (length-k lat acc). Your length-k function should be tail recursive.
So (length2 '(a b c)) returns 3
- Write function (count2 a lat) that returns the number of occurrences of atom a in lat and computes it by calling a continuation-passing style function (count-k n k). Your count-k function should be tail recursive.
So (count2 'x '(a b x d c e x x a x)) returns 4.