;; 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-struct-example) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) #| A Glarp is: - a number, OR - "hmmm", OR - (make-blah [string] [boolean] [Glarp]) * Give 4 examples * give template * write `foo : Glarp -> natnum`, returns the # of 'm's in the given Glarp |# (define-struct blah (name happy? owner)) ; examples of the data 17 "hmmm" (make-blah "rover" #true "hmmm") (make-blah "fido" #false (make-blah "rover" #true "hmmm")) ; template ; func-for-Glarp : Glarp -> ?? ; (define (func-for-glarp a-g) (cond [(number? a-g) ...] [(string? a-g) ...] [(blah? a-g) (... (blah-name a-g) ... (blah-happy? a-g) ... (func-for-glarp (blah-owner a-g)))])) (check-expect (foo 17) 0) (check-expect (foo "hmmm") 3) (check-expect (foo (make-blah "rover" #true "hmmm")) 3) (check-expect (foo (make-blah "fido" #false (make-blah "rover" #true "hmmm"))) 3) (check-expect (foo (make-blah "daisy" #true (make-blah "fido" #false (make-blah "rover" #true "hmmm")))) 3) (check-expect (foo (make-blah "daisy" #true (make-blah "jemal" #false (make-blah "jimmmy" #true "hmmm")))) 7) ; foo : Glarp -> natnum ; returns the # of 'm's in the given Glarp (define (foo a-g) (cond [(number? a-g) 0] [(string? a-g) 3] [(blah? a-g) (+ (count-ms (blah-name a-g)) (foo (blah-owner a-g)))])) (define (count-ms str) (length (filter is-m? (string->list str)))) (define (is-m? c) (char=? #\m c))