;; 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-nonsense) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) (require "student-extras.rkt") #| ; To-model: ; A Glarp is a number, somebody saying "hmmm", OR a blah. ; A Blah contains a name, whether or not its happy, AND a glarp. |# ; datatype-definitions: #| * Give 4 examples * give template * write `foo : (-> glarp natural?)`, which returns the # of 'm's in the given Glarp |# ; examples of the data ; template ; func-for-Glarp : Glarp -> ?? ; (define/contract (func-for-glarp a-g) (-> glarp? ...?) ...) (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) (+ (ms-in-string (blah-name a-g)) (foo (blah-owner a-g)))])) ; ms-in-string : string -> natnum ; How many times does `#\m` occur in `str`? ; (define (ms-in-string str) (length (filter is-m? (string->list str)))) (check-expect (ms-in-string "") 0) (check-expect (ms-in-string "x") 0) (check-expect (ms-in-string "m") 1) (check-expect (ms-in-string "abc") 0) (check-expect (ms-in-string "mmmm") 4) (check-expect (ms-in-string "mm_m_m_") 4) (check-expect (ms-in-string "yummy") 2) ; is-m? : char -> boolean ; Is `c` the character `#\m`? ; ; (We write this purely for showing with `filter`, but w/o an anonymous function. ; IRL, one would absolutely use an anonymous function instead.) ; (define (is-m? c) (char=? #\m c)) (check-expect (is-m? #\m) #true) (check-expect (is-m? #\z) #false)