Problem Set 4
Purpose This problem set (primarily) concerns the design of functions on self-referential data definitions.
You must follow the design recipe. The graders will look for data definitions, contracts, purpose statements, examples/tests, and properly organized function definitions. For the latter, you must design templates. You do not need to include the templates with your homework, however. If you do, comment them out.
You might also note that your graders are now being instructed to grade off for gruesomely laid-out code; proceed accordingly.
Finger Exercises HtDP/2e: 127, 135, 136, 139, 151, 152, 154, 158, 160, 162, 164, 173; in preparation of the next exam, consider solving any of the extended exercises in chapter 13. (As usual, "finger exercises" will not be graded; they are just for you to practice on.)
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). Add an interpretation for the Square and Rectangle classes. Both represent shapes whose borders are parallel to the borders of a canvas (window).
Develop the template for functions that consume Shapes.
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.
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.
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 solving steps ... |
|
;; inspect for expected results: |
(define sh (make-squar 100 100 50 true 'red)) |
(define pt (make-posn 130 130)) |
|
(shape-in? sh pt) |
(shape-draw (make-circl 130 130 5 true 'red) |
(shape-draw sh |
(empty-scene 300 300))) |
Problem 3
Develop the function check-pass-6-10?, which consumes a list of passwords (represented as strings) and produces a boolean indicating whether all are at least 6 characters but no more than 10 characters long.
Generalize the function to check-pass?, which consumes a list of passwords and a minimum and maximum length and produces a boolean indicating whether all passwords are within the allowed length span.
Problem 4
(define-struct ball (x y color)) |
;; Ball = (make-ball Number Number Color) |
;; Color is one of 'red, 'yellow, 'blue, etc. |
Provide a data definition for lists of Balls.
Provide a template for processing such lists.</p>
Design the function lob-length, which counts how many Balls are on a given list of Balls.
Design the function lob-x, which extracts all the x coordinates from a list of Balls.
Design the function lob-draw, which consumes a list of Balls and adds them to an empty scene of 300 x 300 as appropriately colored circles of radius 3.
Design the function lob-filter, which consumes a list of Balls and produces one that contains only those Balls from the given list that are within a 300 x 300 grid.
Design lob-member?. The function consumes a list of Balls, lob, and a Ball b and determines whether b occurs in lob.
Problem 5
The goal of this problem is to develop a component of a slide-show program such as PowerPoint or Keynote. The component displays a single, animated slide. That is, it starts with a plain background and adds phrases to the slide at the rate of one every second.
(define-struct txt (content x y)) |
;; Txt = (make-txt String Number Number) |
|
;; LoTxt is one of: |
;; -- empty |
;; -- (cons Txt LoTxt) |
|
(define-struct world (image hidden)) |
;; World = (make-world Image LoTxt) |
;; intepretation: |
;; The world's image represents the image that the audience can see. |
;; The world's list of Txt represents the yet-to-be-revealed elements. |
Create a world with an empty 400 x 400 canvas to which the program will add the following three phrases: "On your mark.", "Get set.", and "Go!" (at locations that you deem appropriate), which the program will add one step at a time to the canvas.
Design the function display, which consumes a world and returns its current image.
Design the function next, which consumes a world and adds the next hidden Txt to the currently visible slide image. Use 20pt font and blue for the color of the text.
(big-bang WORLD-0 |
(on-tick next 1) |
(to-draw display)) |