Unions
;; A UnionData is one of
;; - Type1
;; - Type2
Example: student
Design data to represent a Northeastern studentās NUID, while taking into account that it might not be known.
;; An NUIDorFalse is one of:
;; - NaturalNumber (Nat)
;; - #false
;; Interpretation: a NEU student's ID or #false if
;; the nuid is not known
(define NDRF-1 12345)
(define NDRF-U #false)
(define (ndrf-template ndrf)
(cond
[(number? ndrf) ... ndrf ...]
[(boolean? ndrf) ...]))
In the template, if it is a number, then we put ... ndrf ... because we can now use that value with something in the function.
Example: Lyft
Design data to represent a Lyft you ordered, which can either be its (x, y) location or a status (such as āarrivedā)
;; A PosnOrString (PorS) is one of:
;; - (make-posn Real Real)
;; - String
;; Interpretation: loation of the car or its status
(define PORS-1 (make-posn 5 10))
(define PORS-2 "arrived")
(define (pors-template pors)
(cond
[(posn? pors) (... (posn-x pors) ...
(posn-y pors))]
[(string? pors) (... pors ...)]))
Example: moon
Design the program moon-stages that waits until the user presses a key, then moves the moon across the night sky, and then shows and end screen
;; A MoonPosition is a one of:
;; - "waiting"
;; - Real
;; - #true
;; Interpretation: stages of the program either
;; waiting for a key, x-pos of the moon, or the program has
;; finished
(define MP-WAITING "waiting")
(define MP-10 10)
(define MP-DONE #true)
(define (mp-temp mp)
(cond
[(string? mp) ...]
[(number? mp) (... mp ...)]
[(boolean? mp) ...]))
;; moon-stages : MoonPosition -> MoonPosition
;; Implements a moon simulation with start/end screens
(define (moon-stages initial-mp)
(big-bang initial-mp
[to-draw draw-mp]
[on-tick move-mp]
[on-key start-mp]))
;; draw-mp : MoonPosition -> Image
;; Visualize the moon position
(check-expect (draw-mp MP-WAITING)
(overlay
(text "Press any key" FONT-SIZE "white")
SKY))
(check-expect (draw-mp MP-DONE)
(overlay
(text "Done!" FONT-SIZE "white")
SKY))
(check-expect (draw-mp MP-10)
(place-image
MOON
10
(/ SKY-HEIGHT 2)
SKY))
(define (draw-mp mp)
(cond
[(string? mp)
(overlay
(text "Press any key" FONT-SIZE "white")
SKY)]
[(number? mp)
(place-image
MOON
mp
(/ SKY-HEIGHT 2)
SKY)]
[(boolean? mp)
(overlay
(text "Done!" FONT-SIZE "white")
SKY)]))
;; move-mp : MoonPosition -> MoonPosition
;; moves the moon, possibly transitions to the end screen
(check-expect (move-mp MP-WAITING) MP-WAITING)
(check-expect (move-mp MP-DONE) MP-DONE)
(check-expect (move-mp MP-10) 20)
(check-expect (move-mp (+ SKY-WIDTH RADIUS)) MP-DONE)
(define (move-mp mp)
(cond
[(string? mp) mp]
[(number? mp)
(if (< mp (+ SKY-WIDTH RADIUS))
(+ mp 10)
MP-DONE)]
[(boolean? mp) mp]))
;; start-mp : MoonPosition KeyEvent -> MoonPosition
;; Handle
(check-expect (start-mp MP-WAITING "a")
(- RADIUS))
(check-expect (start-mp MP-10 "b")
MP-10)
(check-expect (start-mp MP-DONE "C")
MP-DONE)
(define (start-mp mp key)
(cond
[(string? mp) (- RADIUS)]
[(number? mp) mp]
[(boolean? mp) mp]))
(define FONT-SIZE 20)
(define SKY-WIDTH 300)
(define SKY-HEIGHT 200)
(define RADIUS 25)
(define MOON (circle RADIUS "solid" "gray"))
(define SKY (rectangle SKY-WIDTH SKY-HEIGHT "solid" "black"))
Templates
- If there is atomic data to pull out, just use selectors and reference the value
- If some pieces of data have their own data definition, call their template (this means that actual functions will require helpers)
Example: self-referential data with no base case (āno startā)
(define-struct shelfitem [title authors others] )
;; A BookShelf is a (make-shelfitem String String BookShelf)
;; Interpretation: a collection of books on a shelf
;; - title is the title of the book authors is the
;; - author (s) others is the other books on the shelf
The problem here arises when you start to make examples. There needs to be a union to mark the end/start of the book shelf.
Example: return values
;; p-or-f: NaturalNumber -> String
Is this an appropriate signature for a function to determine if a student passed or failed based on their marks?
No, because there are only two possible outcomes, so Boolean would make more sense.