7.5

Homework 3

home work!

Programming Language BSL

Due Date: Saturday January 28, 9pm

Purpose To gain familiarity with structure data and functions that operate on them.

Expectations

This will be an individual (non-pair) assignment. For this and all future assignments you must upload one .rkt file in the language specified at the top of the assignment to the Handin Server. Failure to do so invalidates your submission and will therefore, unfortunately, result in a score of 0.

All functions you write must be accompanied by a signature and a purpose statement, in the format we studied in class, as well as check-expects that exercise the code.

The goal of this problem set is to practice designing data representations using structures and functions for processing such forms of data.

Exercise 1 Here is a data definition for keeping track of time on a clock (which we know is really just a computer with a display attached to it):

(define-struct time [hours minutes])

Use the data definition above for the following exercises:
  • Design the function tock from Time to Time. It adds one minute to the given time, rolling the minutes over from 59 to 0, and the hours from 11 to 0.

  • Design the fun time->text, which converts a time to a text. The text should look like the display of a common alarm clock, i.e., it should separate the minutes from the hours with a colon.

    Does your program display a (make-time 0 17) as 12:17 (which is correct) or as 0:17 (which is not)?

    Hints: a text is an image, not a string, but you will need a string version of the time, too; perhaps you should define a little helper function to make the string you need? See the Racket HelpDesk for information about the image package’s text function. You might also investigate the useful function number->string.

  • Optional: After you have developed these functions, you may wish to add the following lines to your program:
    (big-bang (make-time 0 0)
      [on-tick tock 0.2]
      [to-draw time->text])
    However, if you do, be sure to delete them or comment them out before you turn in your solution. We will subtract points if you leave them in.

Exercise 2 Evaluate the following expressions step by step and write down next to each step whether it is (1) Dr. Racket applying a built-in, primitive function (a "built-in" step), (2) applying a programmer-written function ("plugin" step), or (3) a conditional step:

  • (define (dist-to-O x y)
      (sqrt (+ (* x x) (* y y))))
     
    (dist-to-O 3 4)
  • (define-struct point [x y])
     
    (define (dist-to-O p)
      (sqrt (+ (sqr (point-x p))
               (sqr (point-y p)))))
     
    (dist-to-O (make-point 3 4))
  • (define (step x)
      (cond [(< 1 x)  (sqr x)]
            [(< 0 x)  (* 2 x)]
            [else     (sqr (+ x 1))]))
     
    (step 0)

Exercise 3 A stock brokerage keeps records about stocks with four attributes: name of the company, stock symbol, low price for the day, and high price for the day.

Design a data representation for stocks and a function that computes the average price.