; a snake is ; - symbol name ; - number weight ; - symbol food (define-struct snake (name weight food)) ;skinny-snake?: snake -> boolean ; returns true if snake is less than 10 pounds ;( ... (snake-name s) ... ; ... (snake-weight s) ... ; ... (snake-food s) ...) (define (skinny-snake? s) (< (snake-weight s) 10)) (check-expect (skinny-snake? (make-snake 'frank 15 'donuts)) false) ;feed-ant: ant -> ant ; feeds ant, incrementing weight appropriately (define (feed-ant a) (make-ant (+ (ant-weight a) 0.005) (ant-loc a))) (check-expect (feed-ant (make-ant 0.01 (make-posn 1 3))) (make-ant 0.015 (make-posn 1 3))) ;feed-dillo: dillo -> dillo ; feeds dillo, incrementing weight appropriately (define (feed-dillo a) (make-dillo (+ (dillo-weight a) 0.5) (dillo-dead a))) (check-expect (feed-dillo (make-dillo 4.5 false)) (make-dillo 5.0 false)) ;feed-snake: snake -> snake ; feeds snake, incrementing weight appropriately (define (feed-snake s) (make-snake (snake-name s) (+ (snake-weight s) 13.5) (snake-food s))) (check-expect (feed-snake (make-snake 'bob 4 'donuts)) (make-snake 'bob 17.5 'donuts)) ; a dillo is ; - number weight ; - boolean dead (define-struct dillo (weight dead)) ; run-over-with-car: dillo -> dillo ; sets dead in dillo to true ; template: ; ( ... (dillo-weight d) ... ; ...(dillo-dead d))...) (define (run-over-with-car d) ( make-dillo (dillo-weight d) true)) (check-expect (run-over-with-car (make-dillo 15 false)) (make-dillo 15 true)) ; an ant is: ; - number weight ; - posn loc (define-struct ant (weight loc)) ; ant-at-home: ant -> boolean ; returns true if ant is at (0,0) and false ; otherwise ; template: ; (... ( ant-weight a) ... (ant-loc a) ...) (define (ant-at-home a) (at-zero? (ant-loc a))) (check-expect (ant-at-home (make-ant 0.01 (make-posn 0 0))) true) (check-expect (ant-at-home (make-ant 0.01 (make-posn -1 1))) false) ; at-zero? : posn -> boolean ; returns if posn is (0,0) ; (... (posn-x p) ... (posn-y p) ...) (define (at-zero? p) (and (= (posn-x p) 0) (= (posn-y p) 0) )) (check-expect (at-zero? (make-posn 0 0)) true) (check-expect (at-zero? (make-posn 0 1)) false) ;animal is either: ; ant, or ; dillo, or ; snake ;feed-animal: animal -> animal ;feeds animal, incrementing weight by appropriate amount ; template: ; ...(cond [ (ant? a) ...] ; [ (dillo? a) ...] ; [ (snake? a) ...]) (define (feed-animal a) (cond [ (ant? a) (feed-ant a) ] [ (dillo? a) (feed-dillo a)] [ (snake? a) (feed-snake a)])) (check-expect (feed-animal (make-snake 'bob 4 'donuts)) (make-snake 'bob 17.5 'donuts)) (check-expect (feed-animal (make-dillo 4.5 false)) (make-dillo 5.0 false)) (check-expect (feed-animal (make-ant 0.01 (make-posn 1 3))) (make-ant 0.015 (make-posn 1 3))) ; one data definition: one function ; templates: ; conditional data: cond with one Q per possibility ; compound data: selectors for each piece