ICS H21 • UC IRVINE • DAVID G. KAY • FALL 2009

Lab Assignment 2

This assignment is due at the end of lab on Friday, October 9.

Choose a partner for this assignment, someone you haven't worked with already, and make sure the TA knows who your partner is.

(a) Do exercise 3.3.4 (just the several-function version). Pick two of exercises 4.4.1, 4.4.2, 4.4.3, and 4.4.4. Pick one of exercises 5.1.2, 5.1.3, or 5.1.5. Collect all of these definitions into one file (with both partners' names at the top) and submit it via Checkmate.

(b) Using the image.ss teachpack in DrScheme, we can write functions that process graphic images. Here is some sample code:

```;; Traffic Light example, HtDP Section 6.2, Figure 8
;; Revised to use image.ss teachpack by Matthias Felleisen
(define WIDTH 50)
(define HEIGHT 160)
(define OFFSET 50)

(define RED (circle RADIUS 'solid 'red))
(define YELLOW (circle RADIUS 'solid 'yellow))
(define GREEN (circle RADIUS 'solid 'green))

(define LIGHT-FRAME (rectangle WIDTH HEIGHT 'outline 'black))

(define ALL-THREE-ON
(overlay/xy (overlay/xy (overlay/xy LIGHT-FRAME 0 OFFSET GREEN)
0 0 YELLOW)
0 (- OFFSET) RED))

"All three lights on:"
ALL-THREE-ON
```

Copy this code and run it. Don't forget to add the image.ss teachpack (via the Language menu).

(b.1) Define the images RED-ON, YELLOW-ON, and GREEN-ON, each of which represents a traffic light with just one color light on in the conventional position. Make sure you display each image to see that they look right.

(b.2) Write the function turn-off-red that takes a traffic light image as its input and returns a traffic light image that's the same as the input, except that the red-light position is off (i.e., there's a white circle in the top position). You don't have to check whether the red light (or any other light) is on to start with.

If you test this with (turn-off-red ALL-THREE-ON), you should see a traffic light with the yellow and green lights on. Do that, and then just evaluate the symbol ALL-THREE-ON again. Its value didn't change; it still shows all three colors. Nothing we've done changes the value we associated with ALL-THREE-ON; we just used that value as input to a function, which returned another image, another value based on that input.

If you have experience programming in other languages, you might have expected ALL-THREE-ON to change. That's because your previous language probably uses the imperative programming style, which generally relies on changing the values of variables. But in Scheme, we're programming in the functional programming style. This means that we're not changing the values of variables; instead, we're just sending values as inputs to functions, which return other values. You may wonder how it's even possible to write programs without assigning new values to variables. Well, just stay tuned, and for the moment just remember that the functions we write don't change the values of their inputs.

(b.3) Write the function turn-off-light that takes a traffic light and a symbol ('red, 'yellow, or 'green) and returns the traffic light with the specified color position turned off. Again, you don't have to check the status of the specified light in the input image. Hint: Chapter 5 describes how to design functions that take one of a list of symbols. Design at least one test case for this function (by explicitly creating an expected result and displaying it alongside the result your function produces).

(b.4) Write the function turn-on-light that takes a traffic light and a symbol ('red, 'yellow, or 'green) and returns the traffic light with the specified color position turned on. Again, you don't have to check the input.

(b.5) Write the function switch that takes a traffic light and two symbols (chosen from 'red, 'yellow, or 'green); the function returns a traffic light with the first-specified light turned off and the second-specified light turned on. You don't have to check the input. Hint: In defining new functions, it is always best to employ functions you've already defined rather than re-creating the same functionality from scratch.

(b.6) Now it's time to check the input. Write a predicate function called red-on? that takes a traffic light as input and returns a boolean--true if the red light is on in the input and false if it's off. Hint: Use the function image-inside? from the image.ss teachpack.

(b.7) Write the predicate function traffic-light-working? that takes a traffic light as input and returns a boolean—true if the light is working correctly (i.e., just the red is on, just the yellow is on, or just the green is on) and false otherwise.

(b.8) Write the function next that takes a traffic light as input. If the image doesn't represent a working traffic light, just return the image unchanged. Otherwise, return an image representing the next light in the conventional cycle (a green light becomes yellow; a yellow light becomes red; a red light becomes green). Write tests that check each of these transitions.

Submit your definitions via Checkmate.

(c) Do exercises 6.3.3 and 6.5.2 (on which you should follow the design recipe completely). Submit your definitions via Checkmate.

(d) Do exercise 7.2.1.

For this part, clear all teachpacks and then add the universe.ss teachpack. Do these exercises in section 7.4, which involve shape structures that include a shape's position in a scene: 7.4.1, 7.4.2 (which places the shape's image in an empty scene according to the shape's position), and 7.4.3 (which returns a shape like the input, but with a new position). Then create a world (for use with universe.ss) that holds a shape, write a world-draw function, like the one from last week, that draws this world, and write a world-next function that produces a world with the translated image your function from 7.4.3 creates. Now you can test these functions with universe.ss, using code similar to the code on last week's lab. Finally, notice that exercises 7.4.4, 7.4.5, and 7.4.6 are unnecessary because of what universe.ss does for us automatically.

Do just one of the functions in exercise 7.5.2.

(e) Remember that each partner must complete a partner evaluation form and submit it individually at eee.uci.edu by the end of the day on Friday.

Based in part on ICS H21 assignments by David G. Kay from Fall 2001. Modified for image.ss and world.ss teachpacks by Angelo Pioli and David G. Kay, Fall 2005. Modified by David G. Kay in Fall 2007 and Fall 2009.

David G. Kay, kay@uci.edu

Friday, October 7, 2005 -- 6:25 PM