;; 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_f_full) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) ; max-part : posn -> number ; to return the maximum coordinate of the posn (define (max-part p) (cond [(> (posn-x p) (posn-y p)) (posn-x p)] [else (posn-y p)] )) (check-expect (max-part (make-posn 5 2)) 5) (check-expect (max-part (make-posn -2 1)) 1) ; a snake is: ; (make-snake symbol number symbol) (define-struct snake (name weight food)) ; syntax: struct name, fields names ; The define struct creates for you: ; make-snake ; snake-name ; snake-weight ; snake-food ; snake template: ; (define (fun-for-snake s) ; ...(snake-name s)... ; ...(snake-weight s)... ; ...(snake-food s)...) ; skinny-snake?: snake -> boolean ; returns true if snake is less than 10lbs, false otherwise (define (skinny-snake? s) (< (snake-weight s) 10)) (check-expect (skinny-snake? (make-snake 'tim 4 'rat)) true) (check-expect (skinny-snake? (make-snake 'doug 15 'rat)) false) ; feed-snake : snake -> snake ; feeds the snake a 5lb meal (define (feed-snake s) (make-snake (snake-name s) (+ (snake-weight s) 5) (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?)) ; what names do you get? ; run-over-with-car : dillo -> dillo ; kills a dillo (sadly) (define (run-over-with-car d) (make-dillo (dillo-weight d) true)) (check-expect (run-over-with-car (make-dillo 13 false)) (make-dillo 13 true)) (check-expect (run-over-with-car (make-dillo 11 true)) (make-dillo 11 true)) ; feed-dillo : dillo -> dillo ; feeds a dillo a 2lb meal (define (feed-dillo d) (cond [(dillo-dead? d) d] [else (make-dillo (+ (dillo-weight d) 2) false)])) (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)) ; 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? (make-ant 13 (make-posn 3 0))) false) (check-expect (ant-at-home? (make-ant 11 (make-posn 0 0))) true) ; at-zero? : posn -> boolean ; says whether posn is at the origin (define (at-zero? p) (and (= 0 (posn-x p)) (= 0 (posn-y p)))) (check-expect (at-zero? (make-posn 2 0)) false) (check-expect (at-zero? (make-posn 0 0)) true) ; feed-ant : ant -> ant ; feeds an ant a 1lb meal (define (feed-ant a) (make-ant (+ (ant-weight a) 1) (ant-loc a))) (check-expect (feed-ant (make-ant 12 (make-posn 0 0))) (make-ant 13 (make-posn 0 0))) (check-expect (feed-ant (make-ant 2 (make-posn 0 1))) (make-ant 3 (make-posn 0 1))) ; feed-creature : creature -> creature ; feeds an creature (define (feed-creature a) (cond [(snake? a) (feed-snake a)] [(dillo? a) (feed-dillo a)] [(ant? a) (feed-ant a)])) (check-expect (feed-creature (make-snake 'tim 4 'rat)) (make-snake 'tim 9 'rat)) (check-expect (feed-creature (make-ant 12 (make-posn 0 0))) (make-ant 13 (make-posn 0 0))) (check-expect (feed-creature (make-dillo 12 false)) (make-dillo 14 false)) ; an creature is either: ; - snake, or ; - dillo, or ; - ant ; template for creature: ; (cond [(snake? a) (fun-for-snake a)] ; [(dillo? a) (fun-for-dillo a)] ; [(ant? a) (fun-for-ant a)])) ; A dongwu is either: ; - (make-snake symbol number symbol), or ; - (make-dillo number boolean), or ; - (make-ant number (make-posn number number)) ; -----------lists!------------- ; cons -- constructs a new list (two argument function) ; first -- extracts the first element of a cons list ; rest -- extracts the smaller list from a cons list ; cons? -- tests if something is a list ; a list-of-numbers is: ; - empty ; - (cons number list-of-numbers) ; template for list-of-numbers: ; ...(cond [(empty? lon) ...] ; [(cons? lon) ...(first lon)...(fun-for-list-of-numbers (rest lon)) ]) ; count: list-of-numbers -> number ; counts the number of elements in a list (define (count lon) (cond [(empty? lon) 0] [(cons? lon) (+ 1 (count (rest lon)))])) (check-expect (count (cons 4 (cons 3 (cons 1 empty)))) 3) (check-expect (count empty) 0)