A set may contain elements other than symbols. Develop a parameteric
data definition for sets. Instantiate it for sets of symbols and sets of tuples
of symbols.
Develop polymorphic contracts for elementof, domain,
range, union, and intersect from Problem Set 7(2,3).
Here are the definitions for domain and range:
;; Relation > Set
;; collect all first of
;; the tuples into a set
(define (domain r)
(cond
[(empty? r) empty]
[else (addelement
(first (first r))
(domain (rest r)))]))

;; Relation > Set
;; collect all seconds of
;; the tuples into a set
(define (range r)
(cond
[(empty? r) empty]
[else (addelement
(second (first r))
(range (rest r)))]))

The two functions are obviously similar to each other. Create complementary
tests suites for both and then abstract over them.
An ternary relation is a set that consists of tuples of length 3. Example:
'((a b 1) (a c 2) (b b 1) (a b 3))
The domain is still the set of all firsts in a ternary relation but the range
is the set of all rests. That is, the range is a plain (or binary) relation.
Design the function range3 for ternary relations. Then use the
abstraction from above to define it.
Problem background for the next few problems:
The remaining problems cover the use of abstraction from the perspective of a
game program.
In a "space war" game, the player can fire shots at the UFO as it
descends. Visually a shot is a thin, yellow rectangle. The natural data
representation of a shot is a Posn
; the position represents
the shot's topmost point.
Problem WW1:
Here is the function shotscreate
, which consumes a natural
number and produces a list of Shot
s randomly placed in a 100
x 300 grid:
;; Shot = Posn
;; shotscreate : NaturalNumber > (Listof Shot)
;; create n random Posns
(define (shotscreate n)
(cond
[(zero? n) empty]
[else (cons (makeposn (random 100) (random 300))
(shotscreate (sub1 n)))]))
Turn this into a oneline definition using a "loop".
Problem WW2:
Design the function shotsmove
. It consumes and produces a list
of Shot
s and produces one. Its purpose is to move each shot
upwards (by 3 pixels).
Modify the function so that if a shot is moved outside of the visible scene
(a 100 x 300 grid), it is removed from the resulting list.
Problem WW3:
Design the function hit?
, which consumes a
Rectangle
and a list of shots and determines whether any of
the shots is inside the Rectangle.
(definestruct rectangle (x y width height))
;; A Rectangle
is:
;;  (makerectangle Number Number Number Number)
;; interpretation: the first two numbers specify the
;; northwest (upper left) corner of the rectangle,
;; the last two its width and height. The rectangle
;; is assumed to be parallel to the axes.
Problem WW4:
Design the function draw, which consumes a list of shots and
produces a 100 x 300 scene with those shots. If a shot is only partially
visible, it isn't drawn.
Optional Problem:
Combine the above functions to create a scenery of upwardsflying shots. When
all shots have disappeared, the end of time has come. (See the documentation
for world.ss for an explanation of this phrase.