Problem Set 3h

This is the honors version of Problem Set 3; if you’re looking for the normal version, click here!

image

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. The lab director would like you to design a world program that records a specified number of key strokes with string representation of length 1. That is, the experimenter launches the program on some natural number n and when the “subject”Psychologists use the word “subject” for the victims that participate in their experiments. has struck n keys (with good representations), the program returns the string that is made up of these keys.

The program should draw the current string as a red text with a 40-point font size on a 200 x 50 gray background. For example, if the subject has hit "h", "a", and "t", the program shows

image

Challenge You can’t trust psychologists with computers. Design a checked version of the program that rejects all inputs other than natural numbers.

image

Problem 4 : Cowabunga!

In many parts of the world, UFOs have been abducting cows. This should not surprise you, as you are a UFO pilot who has been sent on just such a mission. As you have probably been warned, however, the base of your UFO is made of a special alloy that cannot be allowed to come in contact with the ground. Your task, therefore, is to land your UFO atop a cow without crashing on Earth... which, as you may have heard, can turn you into a participant in certain "experiments".

How the game works:

A cow is placed at an initial position on the bottom of the screen. The cow moves left at a fixed speed until hitting the left side of the screen at which point it changes direction and moves to the right until hitting the right side, and so on.

A UFO is placed at an initial position at the top of the screen. The UFO moves downward at a fixed rate. The UFO can be moved left/right by pressing the left/right arrow keys.

The game is over when the UFO hits the ground or the cow. The game should indicate in some way whether the game was won (UFO hits cow) or lost (UFO hits ground).

Cows and UFOs

Animation system

Enjoy!