;; a boa is a struct ;; here's how to make an instance of a boa ;; (make-boa string number string) (define-struct boa (name length eats)) ;; a dillo is a struct ;; (make-dillo number boolean) (define-struct dillo (length dead?)) ;; a tiger is a struct ;; (make-tiger string number product) (define-struct tiger (name length sells)) ;; a product is a struct ;; (make-product string string) (define-struct product(item company)) ;; an animal is one of ;; boa ;; dillo ;; tiger ;; fits-cage?: animal number -> boolean ;; consumes an animal and the length of a cage and produces true if the length of the animal is less than the cage length (define (fits-cage? an-ani cage-length) (cond [(boa? an-ani) (< (boa-length an-ani) cage-length) ] [(dillo? an-ani) (< (dillo-length an-ani) cage-length)] [(tiger? an-ani) (< (tiger-length an-ani) cage-length) ])) ;; test cases (check-expect (fits-cage? (make-boa "Fluffy" 12 "rats") 10) false) (check-expect (fits-cage? (make-dillo 4 true) 9) true) (check-expect (fits-cage? (make-tiger "Tony" 35 (make-product "Frosted Flakes" "Kelloggs")) 35) false) ;;; template for functions over animals ; ;;; animal-function: animal ... -> ... ;;; ... ;(define (animal-function an-ani ...) ; (cond [(boa? an-ani) (boa-name an-ani) ; (boa-length an-ani) ; (boa-food an-ani)] ; [(dillo? an-ani) (dillo-length an-ani) ; (dillo-dead? an-ani)] ; [(tiger? an-ani) (tiger-name an-ani) ; (tiger-length an-ani) ; (tiger-sells an-ani)])) ;; has-given-name?: animal string -> boolean ;; consumes an animal and a name and produces true if the animal has the given name ;; (produces false otherwise, or if the animal has no name) (define (has-given-name? an-ani name) (cond [(boa? an-ani) (string=? (boa-name an-ani) name)] [(dillo? an-ani) false] [(tiger? an-ani) (string=? (tiger-name an-ani) name)])) ;; tests (check-expect (has-given-name? (make-boa "Slim" 15 "apples") "Fred") false) (check-expect (has-given-name? (make-dillo 3 false) "Tony") false) (check-expect (has-given-name? (make-tiger "Tony" 35 (make-product "cereal" "Kelloggs")) "Tony") true)