;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname union-of-structs-recursive-example) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) (require racket/contract) (require "student-extras.rkt") ;;;; EXERCISE: ; Steps 1-3 of design recipe for this datatype-definition: ; A Glurp has info: whether happy or not; their age; a name. ; Step 1: datatype definition ;datatype definition: ; (define-struct/c glurp ([is-happy? boolean?] [age non-negative-real?] [name string?])) ; ; interpretation: `age` is in ns. ; or in Java: class Glurp { boolean isHappy; long age; /* ns */ String name; } ; Step 2: Examples ; Examples: ; (define g1 (make-glurp #false 17 "fizzbot")) (define g0 (make-glurp #true 0 "zed")) ; Step 3: ;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 and a pet Blopper), or a jar of sand (only whose weight matters). (define (blooper? val) (or (glurp? val) (schmeek? val) (non-negative-real? val))) ; in g (define-struct/c schmeek ([size non-negative-real?] [pet blooper?])) ; size measured in ;datatype definitions: ; (define (blopper? val) (or (glurp? val) (schmeek? val) (non-negative-real? val))) ; interpretation: the weight of a jar of sand, in g. (define-struct/c schmeek ([size non-negative-real?] [pet blopper?])) ; interp: `size` is in cm. ; Examples: (define blop0 993) ; nearly 1kg of sand (define blop1 g1) (define blop2 (make-schmeek 0 blop0)) (define blop3 (make-schmeek 23.4 blop1)) ; Template: ; (define/contract (func-for-blopper b) (-> blopper? ...?) (cond [(glurp? b) (... (glurp-is-happy? a-glurp) (glurp-age a-glurp) (glurp-name a-glurp))] [(schmeek? b) (... (schmeek-size b) (func-for-blopper (schmeek-pet b)))] [(real? b) (... b)])) ; Step 2: examples of bloppers: g0 g1 37.2 0.0 (make-schmeek 77.7 g0) (make-schmeek 77.7 g1) (make-schmeek 77.7 37.2) (make-schmeek 77.7 0.0) (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))) ;;;;;;;;;;;;;; ; To-model: A car is always driven by a Blooper; 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) (or (false? val) (glurp? val)) ;datatype definition: ; (define-struct/c car ([driver blooper?] [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 ; It's like using a `@Nullable String`, but the type system helps *enforce* ; your code to check for Null (and, has convenience methods).