;; 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 lec5) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ;; A posn is ;; (make-posn num num) ;; An ant is ;; (make-ant num posn) (define-struct ant (weight loc)) ;; ant-at-home? : ant -> bool ;; To determine whether a is at the origin ;(define (ant-at-home? a) ; .... (ant-weight a) ... (posn-at-home? (ant-loc a)) ....) (define (ant-at-home? a) (posn-at-home? (ant-loc a))) ;; posn-at-home? : posn -> bool ;; To deterine whether p is the origin ;(define (posn-at-home? p) ; ... (posn-x p) ... (posn-y p) ....) (define (posn-at-home? p) (and (zero? (posn-x p)) (zero? (posn-y p)))) (check-expect (posn-at-home? (make-posn 3 4)) false) (check-expect (posn-at-home? (make-posn 0 0)) true) (check-expect (ant-at-home? (make-ant 0.001 (make-posn 3 4))) false) (check-expect (ant-at-home? (make-ant 0.007 (make-posn 0 0))) true) ;; feed-ant : ant -> ant ;; To feed a 0.001 lbs of food ;(define (feed-ant a) ; .... (ant-weight a) ... (feed-posn (ant-loc a)) ...) (define (feed-ant a) (make-ant (+ 0.001 (ant-weight a)) (ant-loc a))) ;; feed-posn : posn -> .... ;(define (feed-posn p) ; ... (posn-x p) ... (posn-y p) ...) (check-expect (feed-ant (make-ant 0.001 (make-posn 3 4))) (make-ant 0.002 (make-posn 3 4))) (check-expect (feed-ant (make-ant 0.007 (make-posn 0 0))) (make-ant 0.008 (make-posn 0 0))) ;; move-ant : ant num num -> ant ;; To move the ant a dx in the X direction and dy in the ;; Y direction ;(define (move-ant a) ; .... (ant-weight a) ... (move-posn (ant-loc a)) ....) (define (move-ant a dx dy) (make-ant (ant-weight a) (move-posn (ant-loc a) dx dy))) ;; move-posn : posn num num -> posn ;; To move the posn p by dx and dy ;(define (move-posn p dx dy) ; ... (posn-x p) ... (posn-y p) ....) (define (move-posn p dx dy) (make-posn (+ dx (posn-x p)) (+ dy (posn-y p)))) (check-expect (move-posn (make-posn 3 4) 0 0) (make-posn 3 4)) (check-expect (move-posn (make-posn 2 4) -1 -2) (make-posn 1 2)) (check-expect (move-ant (make-ant 3 (make-posn 3 4)) 7 9) (make-ant 3 (make-posn 10 13))) (check-expect (move-ant (make-ant 0.007 (make-posn 0 0)) 10 3) (make-ant 0.007 (make-posn 10 3))) ;; -------------------------------------------------- ;; FROM LAST TIME: ;; A snake is ;; (make-snake sym num sym) (define-struct snake (name weight food)) ;; snake-skinny? : snake -> boolean ;; To determine whether s is strictly less than 10 lbs ;(define (snake-skinny? s) ; ... (snake-name s) ... (snake-weight s) ... (snake-food s) ...) (define (snake-skinny? s) (< (snake-weight s) 10)) (check-expect (snake-skinny? (make-snake 'Slinky 10 'rats)) false) (check-expect (snake-skinny? (make-snake 'Slimey 8 'grass)) true) (check-expect (snake-skinny? (make-snake 'Hefty 25 'cows)) false) ;; feed-snake : snake -> snake ;; To feed s 5 lbs of food ;(define (feed-snake s) ; .... (snake-name s) ... (snake-weight s) ... (snake-food s) ...) (define (feed-snake s) (make-snake (snake-name s) (+ 5 (snake-weight s)) (snake-food s))) (check-expect (feed-snake (make-snake 'Slinky 10 'rats)) (make-snake 'Slinky 15 'rats)) (check-expect (feed-snake (make-snake 'Slimey 8 'grass)) (make-snake 'Slimey 13 'grass)) ;; A dillo is ;; (make-dillo num bool) (define-struct dillo (weight alive?)) ;; run-over-with-car : dillo -> dillo ;; To (sadly) kill d ;(define (run-over-with-car d) ; ... (dillo-weight d) ... (dillo-alive? d) ...) (define (run-over-with-car d) (make-dillo (dillo-weight d) false)) (check-expect (run-over-with-car (make-dillo 2 true)) (make-dillo 2 false)) (check-expect (run-over-with-car (make-dillo 8 false)) (make-dillo 8 false)) ;; feed-dillo : dillo -> dillo ;; To feed d 2 lds of food, if d is alive? ;(define (feed-dillo d) ; .... (dillo-weight d) .... (dillo-alive? d) ...) (define (feed-dillo d) (cond [(dillo-alive? d) (make-dillo (+ 2 (dillo-weight d)) true)] [else d])) (check-expect (feed-dillo (make-dillo 2 true)) (make-dillo 4 true)) (check-expect (feed-dillo (make-dillo 2 false)) (make-dillo 2 false)) ;; ---------------------------------------- ;; An animal is EITHER ;; - snake ;; - dillo ;; - ant ;; feed-animal : animal -> animal ;; To feed the animal a ;(define (feed-animal a) ; (cond ; [(snake? a) ... (feed-snake a) ...] ; [(dillo? a) ... (feed-dillo a) ...] ; [(ant? a) ... (feed-ant a) ...])) (define (feed-animal a) (cond [(snake? a) (feed-snake a)] [(dillo? a) (feed-dillo a)] [(ant? a) (feed-ant a)])) (check-expect (feed-animal (make-snake 'Slinky 10 'rats)) (make-snake 'Slinky 15 'rats)) (check-expect (feed-animal (make-dillo 5 true)) (make-dillo 7 true)) (check-expect (feed-animal (make-ant 0.001 (make-posn 3 4))) (make-ant 0.002 (make-posn 3 4)))