6.7

Week 2 Set b

home work!

Programming Language BSL

Due Date Wed at 9pm (Week 2)

Purpose To practice solving a simple problem and uploading the solution to the homework server.

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.

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=? "Ben" x) (string-append "Dear " x ", Esquire:")]
    [(string=? "Leena" x) (string-append "Dear " x ", Esquirette:")]
    [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, 4, or 8. Time permitting, solve all of them.

Graded Exercise

Exercise 5 Develop the function qwerty. The function consumes a counting number and extracts that many keyboard characters from the string "qwerty".

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

Exercise 6 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.