Teaching 107 F '08 Assignments The Recipes The Style The Universe The World Subversion Set 1 Set 2 Set 3 Set 4 Set 5 Set 6 Set 7 Set 8 Set 9 Set 10 Set 11

### Problem Set 1

Due date: 9/25

Purpose:

The goal of this problem set is to help you design domain-specific functions, functions that deal with enumerations and unions, functions that deal with `struct`s, and functions that have it all.

#### Drill:

HtDP: 4.2.1, 4.4.1, 5.1.2, 6.3.1, 6.4.1, 6.5.1, 7.2.1, 7.2.2

#### Required Problems:

Note 1: You must use DrScheme's HtDP Beginning Student Language to solve the problems.

Note 2: Changes to "world constants" in the solutions should have no side effects on the workings of the test cases or the workings of the program in problem 2.

1. A car accelerates at 5 meters per square-second like this:

 after t = 0 1 2 3 4 ... 10 ... 15 seconds it has traveled 0 2.5 10 22.5 40 ... ?? ... ?? meters

Your first task is to design the function `how-far`, which, given a number of seconds, computes how far the car has gotten.

Hint: Guess a formula for calculating how far the car has gotten in t seconds. Once the formula works for the first five entries, use it and DrScheme's Interactions Window to fill in the two boxes with ?? in the above table.

Your second task is to design the function `to-image`, which, given a number of seconds, creates a scene (see `World` teachpack) of the a car whose right-most point has gotten as many pixels from the left-most window border as `how-far` dictates. The car's wheel must touch the bottom of the scene.

Develop numeric and image constants that describe the world and the car. You must not use an image of a car, though you may wish to start developing your `to-image` function that way.

Hint: You should tabulate the first few elements of this function just like we tabulated the first few elements of `how-far` for you. Paper and pencil is enough; no need to turn in this table.

2. Design a program that simulates a US traffic light with the World teachpack.

A traffic light that is "off" should be rendered as three colored circles; if one of the bulbs is supposed to be "on", render the corresponding position as a solidly colored disk. For simplicity, make each phase last one second. Make sure that you can change the size of the traffic light with a single change to your program.

Modify the program so that when the viewer presses the space key (key event: `#\space`) the traffic light goes into the fail-safe state of displaying a blinking red light. That is, every other second the red light is on; otherwise all lights are "off." Pressing the space key a second time return the traffic light to its regular function. -- Clearly indicate which portions of the initial solution you had to change.

Domain knowledge: If you do not know how traffic lights are arranged or in what order they work in this country, observe the light on Huntington for a couple of minutes.

3. Design a program that renders an "Enum" as an image:
``````
(define-struct ol (li1 li2 li3))
(define-struct ul (li1 li2 li3))
;; Enum is one of:
;; -- (make-ol String String String)
;; -- (make-ul String String String)
;; interp. each style of enum contains exactly three items
``````
Think of these `enum` as something like an HTML enumeration, ordered (ol) or unordered (ul).

The rendering program consumes an `enum` and a boolean flag, which indicates whether the unordered list (ul) is to rendered with solid circles (•) to the left of each item or a nested unordered list, with an outline circle ο. The ordered list (ol) is always prefixed with natural numbers (1, 2, 3).

Here are three examples:

Hint: Keep in mind the design guideline.

Domain knowledge: The rendering inserts exactly one "string space" between the "enumeration anchor" (number, bullet, circle) and the text of the item. Text is rendered as 11pt and black.

4. Project: The purpose of this exercise is to design a program that computes a ball's perfect bounce on a vertical wall in the middle of a box. The ball moves on straight lines at constant speed, unless it hits the wall, in which case it bounces perfectly. Here are two examples:

Both images show the ball traveling from the red to the blue location. In the image on the left, the ball doesn't bounce. In the image on the right, the ball would travel to the green location, if it weren't for the wall; instead it travels the green part to the wall, flips its horizontal speed, and travels the rest of the distance in the new direction (blue line).

Design a data representation for the ball. Develop constants that describe the world, especially its dimensions and the location of the wall.

Design the function `move`, which computes the next location of the ball assuming it travels for one tick.

Constraints: The wall is a mathematical line, meaning it is drawn with a width of one pixel. Furthermore, the ball cannot move in parallel to the wall and hit the ends of the wall straight-on.

Domain knowledge: Velocity consists of two numbers in a 2d space: one for how far the ball travels horizontally, one for the vertical direction.--- A perfect bounce means that the ball travels to the wall and is then reflected at exactly the same angle as it hits the wall. This reflection is just a negation of the horizontal part of its velocity. From this bounce, it continues its trip for the rest of the distance as if nothing had happened. The overall distance is the same as if it hadn't hit the wall.

 last updated on Tue Jun 9 22:21:18 EDT 2009 generated with PLT Scheme