Assignment 2b
Due Date Thursday 9/14 at 9:00pm
Purpose To practice solving simple problems and combining their answers.
Install DrRacket on your computer. If you don’t own a computer, find a lab machine (first floor WVH for example) where you can log in and launch DrRacket.
Finger Exercises
Exercise 1 Translate the following mathematical function into a BSL function:f(x) = x2 + 12
Use it to create a table for x = 0, 2, 5, and 9.
Exercise 2 Enter the following function definition in BSL into DrRacket:Look up the documentation for expt.Apply the function to 0, 1, and 3 in the interactions area.
Apply the function to 1 in the definitions area and use the stepper to see how DrRacket evaluates this program.
Exercise 3 Enter the following function definition in BSL into DrRacket:
(define (hello x) (cond [(string=? "Ben" x) (string-append "Dear " x ", Esquire:")] [(string=? "Nada" x) (string-append "Dear " x ", Esquirette:")] [else (string-append "Greetings, " x ", ")])) What kind of argument does hello consume? Apply the function to your favorite argument and step through the evaluation.
Exercise 4 Design the function render-string, which consumes a number t and produces a text image of the first t letters from the string "qwerty".
Place the text on a white 200 x 100 rectangle. Use black text of font size 22.
Exercise 5 Design the function add-post. Its task is to create an image that represents a sequence of Piazza posts.
As you can tell, the function consumes three items:
a string, which represents the name(s) of the sender of the latest message;
another string, which represents the latest message; and
an image, which represents what has been said so far and by whom.
The function produces an image that represents the complete history, that is, the history combined with the latest message.There are no constraints on how you wish to represent messages in an image as long as it clearly expresses who said what.
Here is one possible usage scenario:
(define history-1 empty-image) (define history-2 (add-message "Christo and Byron" "hello world" history-1)) (define history-3 (add-message "Ben and Alan" "howdy, y'all" history-2)) (define history-4 (add-message "Becca and Nada" "good bye" history-3)) That is, the image argument to add-post is likely to be something that the function produced before, but it doesn’t have to be so.
Exercise 6 From HtDP 2, 4, or 8. Time permitting, solve all of them.
Graded Exercises
Exercise 7 In case you forgot your grade school terminology, a counting number is 0, 1, 2, 3, 4, 5, ... We will get to the "..." part in a few weeks. Design the function ticket. It consumes two counting numbers: one that represents the speed of a car and the other one the speed limit of the road. The result is one of these strings: (1) "fine" for a car that goes below the speed limit; (2) "danger" for a car that is going at most 5 mph faster than the speed limit; or (3) "ticket!" for a car that exceeds that last speed.
Challenge Instead of issuing "ticket!" for the last case, make the function create the string "you drove ___ mph" where the underlines are replaced by the car’s speed.
Exercise 8 Design a world program that simulates a type writer for the string "qwerty". The simulation ends when the viewer closes the world canvas.
The main function consumes the initial "time" (in seconds) and counts up from there, one tick per second.
Hints:
The display shows the first t letters from the string where t is the current time.
Does anything in this problem imply that you need to handle actual keystrokes typed by the user?
Exercise 9 Design a world program that simulates backspacing on a type writer. The simulation ends when the string is erased.
The main function consumes the to-be-erased string. For example, if the initial string was "FUNDIES", then your simulation should produce the following strings, one per tick: "FUNDIES", "FUNDIE", "FUNDI", "FUND", etc.
Constraint The clock must tick once per second. See on-tick.
Hints
See stop-when.
For each clock tick, one letter disappears from the back of the string.