Problem Set 3h

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


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, 123, 124, 125


Problem 1 Here is a data definition for measuring distances:
(define-struct dist (yards feet inches))
; A Distance is a structure:
;    (make-dist Number Number Number)
; interpretation: (make-dist y f i) is the distance between two
; points expressed in yards, feet, and inches
; Constraints:
;  yards is greater than 0
;  feet is always between 0 and 2
;  inches is always between 0 and 11

Design the function absolute, which converts a Distance to the number of inches it represents.

Design the function plus1, which adds one inch to a given Distance.

Design the function dist->string, which expresses a given Distance as a String. For example, (make-dist 10 2 5) maps to "10yd. 2' 5''".

Problem 2 You have been hired by the local psychology department to assist with the software for a perception experiment. For the first step, the lab director would like you to design a world program that records the first two mouse clicks ("button-down" mouse events) and draws the two clicks and a line between them on the canvas. The experimenter must be able to specify for how many seconds the world program runs.

The program should draw the first mouse click as a five-pointed, solid-red star and the second one as a five-pointed, solid-blue star.

Read the documentation for on-tick to understand how a world program can run for a specified number of seconds.

Challenge Have the program add a green mid-point between the two stars. See Problem Set 2, problem 4 for inspiration.

Problem 3 The second step of your employment is 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 33-point font size on a 100 x 40 gray background. For example, if the subject has hit "a" and "t", the program shows


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

Problem 4 Design the function downward. It consumes a list of strings and produces the string of all first characters in these strings. You do not need to worry about empty strings in the list.


Problem 5 : 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