Problem Set 4
Purpose This problem set (primarily) concerns the design of functions on selfreferential 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 laidout 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 "234871DX"), 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 shapeshiftx. 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 xaxis.
Use the template to design shapein?. 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 shapedraw. The function consumes a Shape, sh and a Scene, sc and adds sh to sc.
;; Shape is one of: 
;;  Circle 
;;  Square 
;;  Rectangle 

(definestruct circl (x y r outline c)) 
;; A Circle is a (makecircl 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 

(definestruct squar (x y size outline c)) 
;; A Square is a (makesquar Number Number Number Boolean Symbol) 
;; interpretation: Supply a good interpretation of Square. 

(definestruct recta (x y width height outline c)) 
;; A Rectangle is a (makerecta Number Number Number Number Boolean Symbol) 
;; interpretation: Supply a good interpretation of Rectangle. 

;; ... problem solving steps ... 

;; inspect for expected results: 
(define sh (makesquar 100 100 50 true 'red)) 
(define pt (makeposn 130 130)) 

(shapein? sh pt) 
(shapedraw (makecircl 130 130 5 true 'red) 
(shapedraw sh 
(emptyscene 300 300))) 
Problem 3
Develop the function checkpass610?, 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 checkpass?, 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
(definestruct ball (x y color)) 
;; Ball = (makeball 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 loblength, which counts how many Balls are on a given list of Balls.
Design the function lobx, which extracts all the x coordinates from a list of Balls.
Design the function lobdraw, 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 lobfilter, 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 lobmember?. 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 slideshow 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.
(definestruct txt (content x y)) 
;; Txt = (maketxt String Number Number) 

;; LoTxt is one of: 
;;  empty 
;;  (cons Txt LoTxt) 

(definestruct world (image hidden)) 
;; World = (makeworld Image LoTxt) 
;; intepretation: 
;; The world's image represents the image that the audience can see. 
;; The world's list of Txt represents the yettoberevealed 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.
(bigbang WORLD0 
(ontick next 1) 
(todraw display)) 