CS 2010 Homework 7   - Due October 8

Setup

There is no setup for this assignment.

Assignment

Exercise 7.1, Using Map

Use map to implement feed-fish-1, which takes a list of numbers and produces a list of numbers that are bigger by one.

Example:

     (feed-fish-1 '(1 2 3)) "should be" '(2 3 4)

Your handin must not contain any definitions except feed-fish-1 before the tests for feed-fish-1.

Hint: This function is easy because the add1 function is built in.

Exercise 7.2, Using Map and Local

Use map to implement feed-fish, which takes a list of numbers and a number to add to element of the list.

Example:

     (feed-fish '(1 2 3) 10) "should be" '(11 12 13)

Your handin must not contain any non-local definitions except feed-fish-1 and feed-fish before the tests for feed-fish.

Hint: As the problem title suggests, you'll need to use local or lambda.

Exercise 7.3, Parallel Number Lists

During the September 15 lecture we implemented a parallel-sum function.

Implement parallel-product, which takes two lists of numbers and returns a list of multiplied numbers:

     (parallel-product '(0 1 2) '(3 4 5)) "should be" '(0 4 10)

Implement parallel-product by abstracting parallel-sum to define parallel-map. Include parallel-product, parallel-sum, and parallel-map in your handin. The parallel-product and parallel-sum functions must be implemented using parallel-map.

You solution must not use the built-in map function.

Exercise 7.4, Parallel-List Contract

Adjust the contract of parallel-map so that the first and second lists can contain any kind of value — and the second list might contain a different kind of value than the first.

Exercise 7.5, Parallel Lists of Different Types

Implement the function flip-some which takes a list of posns and a list of bools, and which produces a list of posns. The result list of posns should be the same as the given list, except that wherever the corresponding boolean is true in the list of bools, the result posn is flipped (see flip-posns).

Example:

     (flip-some (list (make-posn 1 2) (make-posn 3 4))
                (list true false))
     "should be" (list (make-posn 2 1) (make-posn 3 4))

Your implementation must use your parallel-map.

Exercise 7.6, Parallel Lists and Operators

Implement choose which takes two lists of anything (but the same kind of thing in both lists) and an comparison operator. The result is a list containing some elements from the first list and some elements from the second list, depending on the comparison result. When the comparison is true, the result uses the first list's item, and when the result is false, it uses the second lists's item.

Examples:

     (choose '(1 2 3) '(3 2 1) <) "should be" '(1 2 1)
     (choose '(1 2 3) '(3 2 1) >) "should be" '(3 2 3)
     (local [(define (first-is-apple? a b)
               (symbol=? a 'apple))]
       (choose '(apple banana) '(cherry cherry) first-is-apple?))
     "should be" '(apple cherry)

Your implementation must use your parallel-map. Hint: You will also need local or lambda.


Last update: Tuesday, October 7th, 2003
mflatt@cs.utah.edu