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 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. 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 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, '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-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. 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 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 blue 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.
Optional: Make the program run and display the animated slide from above.