RU beehive logo ITEC dept promo banner
ITEC 380
2021fall
ibarland

a non-list example
union of structs w/ recursion

A Glurp has info: whether happy or not; their age;  a name.
   A:  (make-glurp [boolean] [non-negative-real] [string])
       (define-struct glurp (is-happy? age name))

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

       Template:
       (define (func-for-glurp a-glurp)
          (...(glurp-is-happy? a-glurp) ... (glurp-age a-glurp) ... (glurp-name a-glurp)  ))

A Blopper is a Glurp, or a Schmeek (contains a size(cm) and a pet Blopper), or a jar of sand (measure in g)
   A:
       (define nonneg-real? (and/c real? (not/c negative?)))

       ;a Blopper is a glurp or a schmeek, or non-negative-real
       (define blopper? (or/c  glurp?  schmeek?  nonneg-real?)))

       (define-struct schmeek (size pet))
       (make-schmeek [nonneg-real?] [blopper?])



       ; Examples:
       (define blop0 993) ; nearly 1kg of sand
       (define blop1 g17)
       (define blop2 (make-schmeek 0 blop0))
       (define blop3 (make-schmeek 23.4 blop1))


       Template:
       (define (func-for-blopper b)
          (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.



An Optional-Glurp is either a Glurp, or nothing at all.
; Data-definition: a maybe-glurp is
;   - a glurp, or
;   - #false


Task: "A car is always driven by a Blopper; they may bring along one glurp (passenger) if they like."
Ans:
  (define-struct car (driver passenger))
  ; driver : Blopper
  ; passenger : Optional-Glurp

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






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.