RU beehive logo promo banner for Computing & Info Sciences
ITEC 380
2022spring
ibarland

a non-list example
union of structs w/ recursion

(require racket/contract)
(require "student-extras.rkt")

;To-model: A glurp, about which we only care about three things: whether they're happy or not; their age;  their name.


;datatype definition:
;
(define-struct/c glurp ([is-happy? boolean?]  [age non-negative-real?]  [name string?]))
;
; interpretation: `age` is in ns.


; Examples:
;
(define g1 (make-glurp #false 17 "fizzbot"))
(define g0 (make-glurp #true 0 "zed"))



;Template:
;
(define/contract (func-for-glurp a-glurp)
  (-> glurp? ...?)
  ; Return the ... of `a-glurp`.
  (... (glurp-is-happy? a-glurp) (glurp-age a-glurp) (glurp-name a-glurp)))



;;;;;;;;;;;
;
;;;; EXERCISE:
;  Steps 1-3 of design recipe for this datatype-definition:
;
;To-model: a blooper, which is either a Glurp, or a Schmeek (contains a size(cm) and a pet Blopper), or a jar of sand (only whose weight matters).


;datatype definition:
;

;a Blopper is a glurp or a schmeek, or non-negative-real
(define (blopper? val)
  (or  (glurp? val)
       (schmeek? val)
       (non-negative-real? val))) ; interpretation: weight of a jar of sand, in g.

(define-struct/c schmeek ([size non-negative-real?] [pet blopper?]))




; Step 2: examples of bloppers:
;
(define blop0 993) ; nearly 1kg of sand
(define blop1 g1)
(define blop2 (make-schmeek 0 blop0))
(define blop3 (make-schmeek 23.4 blop1))

(make-schmeek 22.2 (make-schmeek 77.7 g1))
(make-schmeek 44.4 (make-schmeek 22.2 (make-schmeek 77.7 g1)))
(make-schmeek 44.4 (make-schmeek 22.2 (make-schmeek 77.7 (make-glurp #t 33.3 "wtf"))))
(make-schmeek 44.4 (make-schmeek 22.2 (make-schmeek 77.7 9999999.9)))




; Template:
;
(define/struct (func-for-blopper b)
    (-> blopper?  ...?)
    (cond [(glurp? b)   (... (glurp-is-happy? a-glurp) (glurp-age a-glurp) (glurp-name a-glurp))]
          [(glurp? b)   (func-for-glurp b)]  ;OR THIS if we already have func-for-glurp templated.
          [(schmeek? b) (... (schmeek-size b) (func-for-blopper (schmeek-pet b)))]
          [(real? b)    (... b)]))


; Note the recursive-call.



;;;;;;;;;;;;;;
;To-model: A car is always driven by a Blopper; they may bring along one glurp (passenger) if they like.

; To represent "a glurp, or not" with a type we'll call the type "optional-glurp".

; datatype definition:
(define (optional-glurp? val)
    ...)


;datatype definition:
;
(define-struct/c car ([driver blopper?]  [passenger optional-glurp?]))

;Examples of data:
(make-car blop2 g1)
(make-car blop0 #false)   ; a car that's being driven by a jar of sand, w/o any passenger.


; Note that `Optional` is in Java:
;    java.util.Optional<String>
; It's like using a `@Nullable String`, but the type system helps *enforce*
; your code to check for Null (and, has convenience methods).





logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.