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

image

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 or equal 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

image

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.