The goal of this problem set is to study the design and processing of
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. 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. -- The
problem set also contains problems concerning computation rules.
HtDP Problems:
9.5.4, 9.5.6, 10.1.7, 10.2.2, 10.2.3
Problem A0
Collect three signatures from tutors. Visit them as a pair during their
office hours (see web page). You must ask for them by name, and you must
ask at least one technical question about the text book. Use a yellow
sticker to mark the page and write down the question. Use a sheet on
which you write down the name of the tutors and they sign next to it. [6 points]
Problem A1:
Evaluate the following expressions step by step and write down next to each
step whether it is (1) arithmetic (of any form), (2) function application
("plugging in") or (3) a conditional step:
(define (fahrenheit->celsius f)
(* 5/9 (- f 32)))
(define (celsius->fahrenheit c)
(+ (* 9/5 c) 32))
(celsius->fahrenheit (fahrenheit->celsius 212))
(define-struct customer (title first last))
(define (formal* loc)
(cond
[(empty? loc) empty]
[else
(cons (formal (first loc)) (formal* (rest loc)))]))
(define (formal c)
(string-append "Dear "
(customer-title c)
" "
(customer-last c)))
(formal* (cons (make-customer "Dr." "Olin" "Shivers")
(cons (make-customer "Mstr." "ZC" "Flatt")
empty)))
Problem A2:
Suppose we have the following class of data:
(define-struct ball (x y color))
;; Ball = (make-ball Number Number Color)
;; Color is one of 'red, 'green, '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 Ball
s.
Provide a template for processing such lists.
Design the function lob-length
, which counts how many
Ball
s are on a given list of Ball
s.
Design the function lob-x
, which extracts all the x
coordinates from a list of Ball
s.
Design the function lob-draw
, which consumes a list of
Ball
s 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
Ball
s and produces one that contains only those
Ball
s from the given list that are within a 300 x 300 grid.
Design lob-intersect
. The function consumes a list of
Ball
s lob
and a Ball
b
. It then determines whether b
occurs on
lob
.
Problem A3:
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.
Here are the data definitions:
(define-struct txt (content x y))
;; Txt = (make-txt Number Number String)
;; 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 still
;; hidden elements
Create a world with an empty blue 300 x 300 canvas to which the program
will add the following three phrases: "Hello Friend", "Nice to See You Again",
and "Until Next Time, Friend", 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.
Optional: Make the program run and display the animated slide from above.