# CSG 107: Problem Set 1

Due: Wednesday, January 14, 2009 at 500pm

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

Remember that you must follow the design recipe. Your deliverables include the data analysis, contract and purpose header, code, and tests. (Remember that there is no deliverable corresponding to the template.)

#### Drill:

All problems are from HtDP unless otherwise specified.

1. Translate the following five intervals on the real line into Scheme functions that accepts a number and return true if the number is in the interval and false if it is outside:

1. the interval [4, 17)
2. the interval (12, 19)
3. the interval (-1, 4]
4. the union of the intervals [1,7] and (12,13)
5. everything outside of the interval (4,6)

2. HtDP, problem 4.4.2
3. 5.1.4
4. 6.3.1
5. Provide data definitions for the following structure definitions. Make appropriate assumptions about what data goes with which field. If you don't know what some of the field names mean, do a little research and find the appropriate domain knowledge.
1. (define-struct student (lastname firstname nuid gpa))
2. (define-struct software-application (manufacturer program-name license-number))
3. (define-struct circle-image (radius color transparency))
4. (define-struct employee (lastname firstname ssn salary))
6. Develop templates for functions that consume each of the structures above.
7. 7.2.1
8. 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 falling object accelerates at 9.8 meters per second per second like this:

 after t = 0 1 2 3 4 ... 10 ... 15 seconds it has traveled 0 4.9 19.6 44.1 78.4 ... ?? ... ?? meters

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

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 a circle whose center has fallen as many pixels from the top edge of the scene as `how-far` dictates. The circle must be tangent to the left edge of the scene.

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 function enum3-to-image that renders an "Enum3" as an image. The formatting of the image is determined by a "Style".
``````
(define-struct enum3 (li1 li2 li3))
;; Enum is one of:
;; -- (make-enum3 String String String)
;; interpretation: an enum3 is a list of exactly three items.

;; Style is one of:
;; -- 'number
;; -- 'roman-numeral
;; -- 'letter
;; -- 'solid-dot
;; -- 'open-dot
``````
Think of these `enum` as something like an HTML enumeration, ordered (ol) or unordered (ul).

The rendering program consumes an `enum` and a style, and produces an image consisting of the items in one of the five styles.

Here are three examples.

the codeproduces
`(enum3-to-image (make-enum3 "hey" "you" "dummy") 'number)`
`(enum3-to-image (make-enum3 "hey" "you" "dummy") 'open-dot) `
`(enum3-to-image (make-enum3 "hey" "you" "dummy") 'solid-dot)`

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 the movement of a ball under the influence of gravity. The ball starts in the upper-left corner of the scene with a fixed horizontal velocity and zero vertical velocity. The ball is accelerated vertically by gravity, but maintains constant horizontal velocity, as dictated by Newton's laws.

The ball continues until it reaches the bottom of the scene, where it takes a perfect bounce. Here is an example:

Design a data representation for the ball.

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

Use the World teachpack to create an animation of the ball.

Domain knowledge: Velocity consists of two numbers in a 2d space: one for how far the ball travels horizontally, one for the vertical direction. Acceleration consists of one number, which alters the velocity at each tick. 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 vertical part of its velocity. From this bounce, it continues its trip for the rest of the distance as if nothing had happened.