6.7

Homework 1

home work!

Programming Language BSL

Due Date Sunday at 11pm

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 fire up DrRacket.

We begin with some simple programming exercises, on numbers and strings.

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:
(define (math-is-boring x)
  (+ (* -4 (expt x 3)) (* 8 (expt x 1)) (* 9 (expt x 0))))
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=? "Olin" x) (string-append "Dear " x ":")]
    [(string=? "Justin" x) (string-append "Hey " x ",")]
    [else (string-append "My dearest " x ", ")]))
What kind of argument does hello consume? Apply the function to your favorite argument and step through the evaluation.

Exercise 4 From HtDP 2, or 4. Time permitting, solve all of them.

Graded Exercises

Exercise 5 Develop the function qwerty. The function consumes a counting number (that is, a non-negative integer) i, and produces the first i characters from the string "qwertyuiop".

What happens when the function is applied to 37? In that case, qwerty just produces the entire string "qwertyuiop". The same happens for all other numbers that are too large.

Exercise 6 Design the function ticket. It consumes two non-negative 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) "ok" 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 7 Design a world program that simulates a typewriter for the string "qwertyuiop". That is, the program uses the big-bang system to show a "movie" of the given string appearing on the movie screen, as if it were being typed at a rate of one keystroke a second. (Students under the age of 45 can look up "typewriters" on wikipedia.)

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.

Clarification: Package up your invocation of the big-bang system in a top-level start-typewriter function. Do not call this function from your program—you invoke it from the interaction pane in Dr. Racket.

Exercise 8 Design a world program that simulates backspacing on a typewriter. The movie begins by showing the entire string qwertyuiop, and then erases the text from the display, moving right to left, at a rate of one character a second. The simulation ends when the string is erased.

For this program, use the string itself as the world state.

Constraint The clock must tick once per second. See on-tick.

Hints See stop-when.

Exercise 9 From HtDP 64

Exercise 10 Consider the following structure-type definition:

(define-struct student [name gpa cs-major?])

Write down the names of the functions (constructors, selectors, and predicates) that it introduces.

Exercise 11 Consider the definition from the previous exercise, and make sensible guesses as to what kind of values go with which fields. Then create at least one instance of the structure.

Exercise 12 Write the function on-axis? that consumes a posn struct, and returns true if the point represented by the posn is on the x-axis or the y-axis.