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.
Problem A1:
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 A2:
A weather station measures the temperature in degrees Fahrenheit every day at noon and midnight (actually the second measurement is taken at 11:59pm on the same calendar day). The temperatures are saved in two lists, one list for noon and one for midnight. Develop the function anomaly
, which consumes the two temperature lists and outputs a list of booleans, true
if the corresponding midnight temperature is greater than the one for noon.
Problem A3:
Develop the function cesarify
which consumes a list of symbols and returns the same list but with every instance of 'pizza
doubled. For example,
(cesarify (cons 'wurst (cons 'huevos (cons 'pizza (cons 'pants empty)))))
would be expected to return
(cons 'wurst (cons 'huevos (cons 'pizza (cons 'pizza (cons 'pants empty)))))
Problem A4:
The 2htdp/image
teachpack contains many functions which create images of simple geometric figures: circle, ellipse, line, triangle,
and so on. Provide a data definition for a catalog where each entry names a figure and also contains a corresponding example. It should be possible to make catalogs containing any number of figures. Show how to use this data definition to construct a specific sampler catalog of at least five different figures.
Now develop the function show-example
, which consumes the name of a figure (represented as a symbol) and the sampler. It produces the corresponding sample image or false
if the named figure was not in the sampler.
Problem A5:
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 A6:
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 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-member?
. The function consumes a list of
Ball
s, lob
, and a Ball
b
and determines whether b
occurs in
lob
.
Problem A7:
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 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. Something like this might help...
(big-bang WORLD-0
(on-tick next 1)
(to-draw display))