;; 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-beginner-reader.ss" "lang")((modname week2_w_full) (read-case-sensitive #t) (teachpacks ((lib "image.ss" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.ss" "teachpack" "2htdp"))))) ; max-part : posn -> number ; to return the maximum coordinate of the posn ; template: ; ...(posn-x p)... ; ...(posn-y p)... (define (max-part p) (max (posn-x p) (posn-y p) )) (check-expect (max-part (make-posn 2 3)) 3) (check-expect (max-part (make-posn -1 -4)) -1) ; Data definition: ; a snake is: ; - (make-snake symbol number symbol) (define-struct snake (name weight food)) ; syntax: struct name, fields names ;Creates four functions: ; (make-snake a b c) ; (snake-name s) ; (snake-weight s) ; (snake-food s) ; (snake? s) ; skinny-snake?: snake -> boolean ; returns true if snake is less than 10lbs, false otherwise ; ...(snake-name s)... ; ...(snake-weight s)... ; ...(snake-food s)... (define (skinny-snake? s) (< (snake-weight s) 10) ) (check-expect (skinny-snake? (make-snake 'bob 13 'mice)) false) (check-expect (skinny-snake? (make-snake 'jill 9 'mice)) true) ; feed-snake : snake -> snake ; feeds the snake a 5lb meal (define (feed-snake s) (make-snake (snake-name s) (+ 5 (snake-weight s)) (snake-food s) )) (check-expect (feed-snake (make-snake 'tim 4 'rat)) (make-snake 'tim 9 'rat)) (check-expect (feed-snake (make-snake 'jill 9 'mouse)) (make-snake 'jill 14 'mouse)) ; a dillo is ; (make-dillo number boolean) (define-struct dillo (weight dead?)) ;template: ; ...(dillo-weight d) ; ...(dillo-dead? d) ; feed-dillo : dillo -> dillo ; feeds a dillo a 2lb meal (define (feed-dillo d) (make-dillo (cond [(dillo-dead? d) (dillo-weight d)] [else (+ 2 (dillo-weight d))]) (dillo-dead? d) )) (check-expect (feed-dillo (make-dillo 12 false)) (make-dillo 14 false)) (check-expect (feed-dillo (make-dillo 11 true)) (make-dillo 11 true)) ; an ant is ; - (make-ant number posn) (define-struct ant (weight loc)) ;template: ;...(ant-weight a) ;...(ant-loc a) (define ant1 (make-ant 0.01 (make-posn 0 0))) (define ant2 (make-ant 0.005 (make-posn 2 3))) ; ant-at-home? : ant -> boolean ; returns whether ant is at home (the origin) (define (ant-at-home? a) (at-zero? (ant-loc a)) ) (check-expect (ant-at-home? ant1) true) (check-expect (ant-at-home? ant2) false) ; at-zero? : posn -> boolean ; returns whether posn is origin (define (at-zero? p) (and (= 0 (posn-x p)) (= 0 (posn-y p)) ) ) (check-expect (at-zero? (make-posn 0 1)) false) (check-expect (at-zero? (make-posn 0 0)) true) ; feed-ant : ant -> ant ; feeds ant a 0.01lb meal (define (feed-ant a) (make-ant (+ (ant-weight a) 0.01) (ant-loc a)) ) (check-expect (feed-ant ant1) (make-ant 0.02 (make-posn 0 0))) ; An animal is either: ; - snake, or ; - dillo, or ; - ant ; Template: ; (cond [(snake? a) ...] ; [(dillo? a) ...] ; [(ant? a) ...] ; ) ;Example: (make-snake 'bob 10 'fries) ;feed-animal : animal -> animal ; feeds the animal appropriate-sized meal (define (feed-animal a) (cond [(snake? a) (feed-snake a)] [(dillo? a) (feed-dillo a)] [(ant? a) (feed-ant a)] ) ) (check-expect (feed-animal ant1) (make-ant 0.02 (make-posn 0 0))) (check-expect (feed-animal (make-snake 'tim 4 'rat)) (make-snake 'tim 9 'rat)) (check-expect (feed-animal (make-dillo 11 true)) (make-dillo 11 true)) ;a dongwu is either: ; (make-snake symbol number symbol), or ; (make-dillo number boolean), ; (make-ant number (make-posn number number)) ; (cond [(snake? d) ...(snake-name d)...(snake-weight d)...(snake-food d)] ; [(dillo? d) ...(dillo-weight d)...(dillo-dead? d)] ; [(ant? d) ...(ant-weight d)...(ant-loc d)])