CS 1410-20 Homework 7

Due: Friday, October 22nd, 2010 9:40am

Part 1 – Using Map

Use map to implement numbers->strings, which takes a list of numbers and produces a list of strings, where each number is converted to its string form. For example, (numbers->strings '(1 2 3)) should produce '("1" "2" "3").

Your handin must not contain any definitions except numbers->strings before the tests for numbers->string.

Hint: This function is easy because the number->string function (which converts a single number to a string) is built in.

Part 2 – Using Map and Local

Use map to implement percents->strings, which takes a list of numbers and produces a list of strings, where each number is divided by 100 and converted to its string form. For example, (percents->strings '(10 20 300)) should produce '("1/10" "1/5" "3").

Your handin must not contain any non-local definitions except numbers->strings and percents->strings before the tests for percents->string.

Hint: As the part name suggests, you’ll need to use local or lambda.

Part 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. For example, (parallel-product '(0 1 2) '(3 4 5)) should produce '(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.

Part 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.

Part 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).

For example, (flip-some (list (make-posn 1 2) (make-posn 3 4)) (list true false)) should produce (list (make-posn 2 1) (make-posn 3 4)).

Your implementation must use your parallel-map.

Part 6 – Parallel Lists and Operators

Implement choose which takes two lists of anything (but the same kind of thing in both lists) and a 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.

For example, (choose '(1 2 3) '(3 2 1) <) should produce '(1 2 1), and (choose '(1 2 3) '(3 2 1) >) should produce '(3 2 3). Finally,

  (local [(define (first-is-apple? a b)
            (symbol=? a 'apple))]
    (choose '(apple banana) '(cherry cherry) first-is-apple?))

should produce '(apple cherry).

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


Last update: Wednesday, October 20th, 2010
mflatt@cs.utah.edu