Problem Set 3

home work!

Programming Language BSL

Purpose The purpose of this problem set is to internalize design skills. Topically, the problem set focuses on functions that process structures and arbitrary unions of data.

Finger Exercises HtDP/2e: 65, 67, 68, 69, 70, 71, 77, 78, 79, 86, 94, 102, 104

image

Problem 1 Consider the following structure definitions:
(define-struct lecture-hall (number capacity))
(define-struct automobile (year make model))
(define-struct football-player (name position number))
(define-struct shirt (material size color))

  1. What are the names of the constructors and the selectors that each of the structures adds to Racket? Draw box representations for each of these structures.

  2. Provide data definitions for the structure definitions above. Make appropriate assumptions about what data goes with which field.

  3. Develop templates for functions that consume the structures above.

Problem 2 Here is a data definition for measuring time:
(define-struct time (hours minutes))
; A Time is a structure:
;    (make-time Number Number)
; interpretation: (make-time h m) is the time  
; expressed in hours, and minutes
; Constraints:
;  hours is always between 0 and 11
;  minutes is always between 0 and 59

  1. Design the function tock, which adds one minute to the given time.

  2. Design the function time->text, which converts a time to a text. The text should look like the display of a common alarm clock, i.e., it should separate the minutes from the hours with a colon. Hint: a text is an image, not a string, but you will need a string version of the time, too. See the HelpDesk for more on the text function.

  3. After you have developed these functions, add a main function, which launches a big-bang program.

Problem 3 You have been hired by the local psychology department to assist with the software for a perception experiment. They would like you to develop a program that helps people control the movement of a ball across the screen.

The ball can move on straight lines only, that is, up, down, left or right. Pressing the arrow keys on the keyboard changes the ball’s location.

  1. Develop a data representation for the current position of the ball. The position is best described with a pair of positive integers.

  2. Develop a data representation for the speed and direction of the ball. You may assume that the ball always moves exactly 10 pixels at a time.

  3. Develop a data representation for the ball.

  4. Design the function ball-next, which consumes the representation of a ball and creates a ball that represents where it will be after one tick.

  5. Design the function ball-image, which consumes representation of a ball and produces a rectangle of 300x300 pixels with a red dot (diameter 10 pixels) placed at the ball’s position.

  6. Design the function ball-change, which consumes a ball and a key-event which represents the user hitting a key on the keyboard. If the key event represents the user hitting the up-arrow key, then ball-change produces a new ball which is just like the input ball, except that the new ball is moving upward; similarly for key events representing the left-, right- and down-arrow keys: they cause the resulting ball to be one that is moving left, right, or down, respectively. Passing any other keystroke to ball-change causes the ball to be unchanged. Reading about key-events in the HelpDesk is a good place to start.

  7. After you have developed these functions, add a main function, which launches a big-bang program.