(define-struct boa (name length food)) ;; a Boa is a (make-boa String Natural String) ;; interp: a boa constrictor, where ;; name is the boa's name ;; length is its length (in inches) ;; food the the boa's favorite food ;; examples (define MARY (make-boa "Mary" 37 "ferns")) (define-struct dillo (length dead?)) ;; A Dillo is a (make-dillo Natural Boolean) ;; interp: an armadillo where ;; length is the dillo's length (in inches) ;; dead? is true if the dillo is dead (make-dillo 12 false) (define MAX (make-dillo 14 true)) ;; provide a data definition for a tiger that has a name, a length, ;; and a product that it sells. A product is comprised of the item, ;; and the manufacturer (define-struct product (item manufacturer)) ;; a Product is a (make-product String String) ;; interp: a product where ;; item is the name of the item ;; manufacturer is the manufacturer (make-product "Frosted Flakes" "Kelloggs") ;;; fcn-for-product: Product -> ;;; ;(define (fcn-for-product a-prod) ; (... (product-item a-prod) ; (product-manufacturer a-prod))) (define-struct tiger (name length sells)) ;; a Tiger is a (make-tiger String Natural Product) ;; interp: a tiger where ;; name is the tiger's name ;; length is its length (in inches) ;; sells is a product that the tiger sells (make-tiger "Tony" 72 (make-product "Frosted Flakes" "Kelloggs")) ;;; fcn-for-tiger: Tiger -> ;;; ;(define (fcn-for-tiger a-tiger) ; (... (tiger-name a-tiger) ; (tiger-length a-tiger) ; (fcn-for-product (tiger-sells a-tiger)))) ;; an Animal is one of ;; Boa ;; Dillo ;; Tiger ;;; fcn-for-animal: Animal -> ;;; ;(define (fcn-for-animal 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) (fcn-for-tiger an-ani)])) ; ;; sells-gas?: Animal -> Boolean ;; consumes an animal and produces true if the item that the animal sells is ;; gas. If the animal doesn't sell a product, the function returns false (define (sells-gas? an-ani) (cond [(boa? an-ani) false] [(dillo? an-ani) false] [(tiger? an-ani) (tiger-sells-gas? an-ani)])) ;; test (check-expect (sells-gas? (make-boa "Slim" 19 "pickles")) false) (check-expect (sells-gas? (make-tiger "Tony" 70 (make-product "Frosted Flakes" "Kelloggs"))) false) ;; tiger-sells-gas?: Tiger -> Boolean ;; consumes a tiger and produces true if the item the tiger sells is gas (define (tiger-sells-gas? a-tiger) (... (item-is-gas? (tiger-sells a-tiger)))) ;; item-is-gas?: Product -> Boolean ;; produces true if the item in the product is gas (define (fcn-for-product a-prod) (... (product-item a-prod) (product-manufacturer a-prod)))