Homework 5
Due Date: Saturday February 11, 9pm
Purpose
The goal of this problem set is to study the design and processing of inductively defined, self-referential unions. We focus on lists for now. Soon you will see that the design recipe applies to all forms of self-referential data definitions. You must follow the design recipe in your solutions: 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.
![]()
HtDP/2e Problems: 163, 164, 165, 166, 167
(define-struct ball [x y color]) ; Ball = (make-ball Number Number Color) ; Color is one of 'red, 'yellow, 'blue, etc.
Think of instances of ball as a Cartesian point, specifying where the ball is located, and the color of the ball.
Provide a data definition for lists of Balls.
Provide a template for processing such lists.
Design the function lob-length, which counts how many Balls are on a given list of Balls.
Design the function lob-xs, 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-visibles, 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-contains?. The function consumes a list of Balls, lob, and a Ball, b, and determines whether or not b occurs in lob.
Problem 2
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!", which the program will add one step at a time to the canvas. Make sure that a single change to your program changes the placements of all phrases on the slide.
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) (on-draw display))
