Problem Set 4

home work!

Programming Language BSL

Purpose This problem set concerns the design of functions on that process structures and arbitrary itemizations (unions) of data.

You must follow the design recipe. The graders will look for data definitions, signatures, purpose statements, examples/tests, and properly organized function definitions. For the latter, you must design templates.

Finger Exercises HtDP/2e: 87, 88, 89, 90, 115, 116, 117, 118, 121, 123, 124, 125, 127, 128; (As usual, "finger exercises" will not be graded; they are just for you to practice on.)

image

Problem 1 A movie store sells two kinds of movies: regular and classic. For regular movies, they track the product id (a string, such as "234-87-1DX"), its base price, and the number of years it has been in their collection. For each year a movie is in stock, it is marked down by 3.5% of the base price, but no movie is sold for less than $2. For classic movies, they track the product id (again, represented as a string), and its price, which is never discounted.

Design a data representation for the store’s items and a function that computes the current price of an item. (Think of the program in a cash register and how it computes the current price from the price tag.)

Problem 2 Below is a data definition for a class of shapes (See the code at the bottom).

  1. Add an interpretation for the Square and Rectangle classes. Both represent shapes whose borders are parallel to the borders of a canvas (window).

  2. Develop the template for functions that consume Shapes.

  3. Use the template to design shape-shift-x. The function consumes a Shape, sh, and a number, delta. It produces a shape that is like sh but shifted by delta pixels along the x-axis.

  4. Use the template to design shape-in?. The function consumes a Shape, sh, and a Posn, p, and determines whether p is inside (or on the boundary) of sh.

    Domain Knowledge: for a point to be within a circle, its distance to the center must be less than or equal to the circle’s radius. For a point to be within a rectangle, its x coordinate must be between the x coordinate of the left line and the x coordinate of the right line. How do you compute the x coordinates of these lines? Naturally something analogous must hold for the y coordinates. Remember that squares are just special rectangles.

  5. Use the template to design shape-draw. The function consumes a Shape, sh and a Scene, sc and adds sh to sc.

;; Shape is one of:

;; -- Circle

;; -- Square

;; -- Rectangle

 

(define-struct circl (x y r outline c))

;; A Circle is a (make-circl Number Number Number Boolean Symbol)

;; interpretation: x and y determine the center of the circle,

;;   r the radius, outline whether it's outlined or solid,

;;   and c its color

 

(define-struct squar (x y size outline c))

;; A Square is a (make-squar Number Number Number Boolean Symbol)

;; interpretation: Supply a good interpretation of Square.

 

(define-struct recta (x y width height outline c))

;; A Rectangle is a (make-recta Number Number Number Number Boolean Symbol)

;; interpretation: Supply a good interpretation of Rectangle.

Problem 3 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.