;; 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 ()))) ;; ======================================== ;; From here until the next line of "===="s ;; is simply copied from the previous lecture. ;; A snake is ;; (make-snake sym num sym) (define-struct snake (name weight food)) ;; snake-skinny? : snake -> boolean ;; To determine whether s is < 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 'Slimey 10 'rats)) false) (check-expect (snake-skinny? (make-snake 'Slinky 1 'donuts)) true) (check-expect (snake-skinny? (make-snake 'Smokey 20 'steak)) 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 'Slimey 10 'rats)) (make-snake 'Slimey 15 'rats)) (check-expect (feed-snake (make-snake 'Slinky 1 'donuts)) (make-snake 'Slinky 6 'donuts)) ;; ---------------------------------------- ;; A dillo is ;; (make-dillo num bool) (define-struct dillo (weight alive?)) ;; run-over-with-car : dillo -> dillo ;; To (unfortunately) 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 5 true)) (make-dillo 5 false)) (check-expect (run-over-with-car (make-dillo 6 false)) (make-dillo 6 false)) ;; feed-dillo : dillo -> dillo ;; To feed d 2 lbs of food, if it's 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 5 true)) (make-dillo 7 true)) (check-expect (feed-dillo (make-dillo 6 false)) (make-dillo 6 false)) ;; ======================================== ;; Starting here is the new code for this lecture. ;; An ant is ;; (make-ant num posn) (define-struct ant (weight loc)) ;; A posn is ;; (make-posn num num) ;; feed-ant : ant -> ant ;; Feeds the ant a 0.001 lbs of food ;(define (feed-ant a) ; ... (ant-weight a) ; ... (feed-posn (ant-loc a)) ; ....) (define (feed-ant a) (make-ant (+ (ant-weight a) 0.001) (ant-loc a))) ;; feed-posn : 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 8 2))) (make-ant 0.008 (make-posn 8 2))) ;; move-ant : ant num num -> ant ;; To move the ant a by dx and dy ;(define (move-ant a dx dy) ; (ant-weight a) ; (move-posn (ant-loc a) dx dy) ; dx ... dy ; ...) (define (move-ant a dx dy) (make-ant (ant-weight a) (move-posn (ant-loc a) dx dy))) ;; move-posn : posn num num -> posn ;; Move the posn p by dx 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 2 3) 5 7) (make-posn 7 10)) (check-expect (move-ant (make-ant 0.001 (make-posn 3 4)) 5 7) (make-ant 0.001 (make-posn 8 11))) (check-expect (move-ant (make-ant 0.007 (make-posn 8 2)) -5 12) (make-ant 0.007 (make-posn 3 14))) ;; ---------------------------------------- ;; An animal is either ;; - snake ;; - dillo ;; - ant ;; feed-animal : animal -> animal ;(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)])) ;; In the general case, we'd have templates and ;; functions for `feed-snake', `feed-dillo', and ;; `feed-ant' (with its `feed-posn') here, but ;; we've already done those above. (check-expect (feed-animal (make-snake 'Slinky 10 'rats)) (make-snake 'Slinky 15 'rats)) (check-expect (feed-animal (make-dillo 2 true)) (make-dillo 4 true)) (check-expect (feed-animal (make-ant 0.002 (make-posn 3 4))) (make-ant 0.003 (make-posn 3 4)))