Repetition Potpourri


Due: 11 a.m., Wednesday, 15 October 2008

Summary: Practice three techniques for repetition you learned this week.

Purposes: Gain additional experience with compose, interesting uses of image-compute-pixels!, and basic list recursion.

Expected Time: Two to three hours.

Collaboration: We encourage you to work in groups of size three. You may, however, work alone or work in a group of size two or size four. You may discuss this assignment with anyone, provided you credit such discussions when you submit the assignment.

Submitting: Email your answer to . The title of your email should have the form HW6 and should contain your answers to all parts of the assignment. Scheme code should be in the body of the message.

Warning: So that this assignment is a learning experience for everyone, we may spend class time publicly critiquing your work.

Preliminaries

This assignment has three parts, based on three topics from the past week. The three parts are independent and may be completed in any order.

Assignment

Part A: Image filters

1. Implement a procedure (image-through-rose-colored-glasses image weight). This procedure should take an image and a weight as parameters and produce a new image where each pixel is the weighted average of the color from the original image and the color named rose. Consider the following examples using Professor Davis's StalkerNet image:

Originalweight = 0.3weight = 0.7

We have several hints for you:

  • Use image-variant to produce the new image.
  • In homework 4, you implemented the rgb-weighted-average procedure. Think about how to make use of that procedure here.
  • At some point you will need to convert the color name “rose” to an RGB value. This operation is slow and could have a significant effect on the efficiency of your filter. Think about where and when you will make this conversion to let the filter run as quickly as possible.

2. Generalize your procedure by allowing the user to specify what color to use for the filter. Write a new procedure, (image-color-filter image weight colorname). The third parameter should be a string representing the name of a color.

Here are a few examples.

Originalweight = 0.5, colorname = “sky blueweight = 0.7, colorname = “sea green

Part B: Creating interesting (?) images with image-compute-pixels!

3. Build the ugliest image you can (in one hour or less) using the ideas from the lab on building images by iterating over positions. Your image should include at least one blend, at least one striped shape, and at least one shape that is not a square or rectangle.

Part C: Basic list recursion

4. Imagine that map were not included in Scheme. Could you implement it yourself? Certainly! The map procedure can be implemented recursively. In fact, this is a procedure that we implemented ourselves for DrFu.

Using what you learned about list recursion this week, implement the procedure (my-map proc lst). Like map, your procedure should take as parameters a unary procedure and a list, and produce a new list that contains the results of applying the procedure to each item in the original list. Please document my-map using the 6 P's format.

5. In the lab on recursion basics, we asked you to implement a procedure (rgb-tally-dark colors) that, given a list of colors, counts how many are dark. How might we generalize this idea? A more general procedure might take both a list and a predicate as parameters, and count the number of items in the list for which the predicate is true.

Implement a procedure (tally pred? lst) that takes as parameters pred?, a predicate procedure, and lst, a list of values. The procedure should return a non-negative integer representing the number of items in lst for which pred? is true.

For example,

> (tally even? (list 1 2 3 4 5))
2
> (tally odd? (list 1 2 3 4 5))
3
> (tally integer? (list 1 2 3 4 5))
5
> (tally symbol? (list 1 2 3 4 5))
0

Important Evaluation Criteria

In evaluating your work, we will consider the correctness, conciseness, and elegance of your code and the documentation we asked you to write. In awarding pluses, we will also look at the creativity of your code in Part B; did you go beyond what was demonstrated in the lab?

Extra Credit Opportunity: Splitting Lists

Define and test a Scheme procedure, (unriffle lst), that takes a list as its parameter and produces a list of two lists, one comprising the elements in even-numbered positions in the given list, the other comprising the elements in odd-numbered-positions.

> (unriffle (list 'a 'b 'c 'd 'e 'f 'g 'h 'i))
((a c e g i) (b d f h))
> (unriffle (list))
(() ())
> (unriffle (list 'a))
((a) ())
> (unriffle (list 'b))
((b) ())
> (unriffle (list 'a 'b))
((a) (b))

Hint: There are many ways to solve this problem. Most of them involve a recursive helper procedure.


Jerod Weinman