;; The first three lines of this file were inserted by DrScheme. 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 lect03) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "world.ss" "teachpack" "htdp"))))) (define-struct dillo (name length alive?)) ; make-dillo : string number boolean -> dillo ; length is in meters. ; scheme auto-creates four methods: ; a consttructor ; make-dillo : string number boolean -> dillo ; and three accessors ; dillo-name : dillo -> string ; dillo-length : dillo -> number ; dillo-alive? : dillo -> boolean (define-struct boa (name length num-spots)) (define (grow-boa boa1) (make-boa (boa-name boa1) (+ 0.1 (boa-length boa1)) (boa-num-spots boa1))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Union data ;; An animal is: ;; - a boa, or ;; - a dillo. ; grow : animal -> animal ; (define (grow an-animal) (cond [(boa? an-animal) (grow-boa an-animal)] [(dillo? an-animal) (make-dillo (dillo-name an-animal) (+ 0.2 (dillo-length an-animal)) (dillo-alive? an-animal))] )) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #;(check-expect (draw-droplet (make-drop 20 50) (empty-scene 200 100)) (place-image (empty-scene 200 100) 20 50 (circle 10 'solid 'blue))) ; A list-of-numbers is: ; - empty or ; - (cons [number] [list-of-numbers]) ; ;(define (fun-for-list nums) ; (cond [(empty? nums) ...] ; [(cons? nums) (first nums) ...(fun-for-list (rest nums))... ])) ; count-evens : list-of-numbers -> number ; (define (count-evens nums) (cond [(empty? nums) 0] [(cons? nums) (+ (count-evens (rest nums)) (if (even? (first nums)) 1 0))])) (check-expect (count-evens empty) 0) (check-expect (count-evens (cons 3 empty)) 0) (check-expect (count-evens (cons 4 empty)) 1) (check-expect (count-evens (cons 3 (cons 4 empty))) 1) (check-expect (count-evens (cons 4 (cons 3 empty))) 1) ; filter-evens : list-of-nums -> list-of-nums (define (filter-evens nums) (cond [(empty? nums) empty] [(cons? nums) (if (even? (first nums)) (cons (first nums) (filter-evens (rest nums))) (filter-evens (rest nums)))])) (check-expect (filter-evens empty) empty) (check-expect (filter-evens (cons 3 empty)) empty) (check-expect (filter-evens (cons 4 empty)) (cons 4 empty) ) #;(check-expect (filter-evens (cons 3 (cons 4 empty))) ...) #;(check-expect (filter-evens (cons 4 (cons 3 empty))) ...) (check-expect (filter-evens (list 13 42 99 0 23 2 19 4 4 4)) (list 42 0 2 4 4 4)) (check-expect (filter-evens (list 100 13 42 99 0 23 2 19 4 4 4)) (list 100 42 0 2 4 4 4)) ; make-spotted-boas : list-of-number -> list-of-boas ; (define (make-spotted-boas nums) (cond [(empty? nums) empty] [(cons? nums) (cons (make-boa "hissy" 10 (first nums)) (make-spotted-boas (rest nums)))])) (check-expect (make-spotted-boas empty) empty) (check-expect (make-spotted-boas (cons 4 (cons 93 empty))) (cons (make-boa "hissy" 10 4) (cons (make-boa "hissy" 10 93) empty))) (check-expect (make-spotted-boas (cons 30 (cons 4 (cons 93 empty)))) (cons (make-boa "hissy" 10 30) (cons (make-boa "hissy" 10 4) (cons (make-boa "hissy" 10 93) empty)) )) ; A list-of-robot-moves is either: ; - empty ; - (cons number list-of-robot-moves) ; - (cons symbol list-of-robot-moves) ; examples of the data: empty (cons 1 empty) (cons 'left empty) (cons 2 (cons 1 empty)) (cons 'right (cons 2 (cons 1 empty))) (cons 1 (cons 'right (cons 2 (cons 1 empty)))) ; robot-steps: list-of-robot-moves -> number (define (robot-steps lorms) (cond [(empty? lorms) 0] [(number? (first lorms)) (+ (first lorms) (robot-steps (rest lorms)))] [(symbol? (first lorms)) (robot-steps (rest lorms))])) (check-expect (robot-steps empty) 0) (check-expect (robot-steps (cons 1 empty)) 1) (check-expect (robot-steps (cons 2 (cons 1 empty))) 3) (check-expect (robot-steps (cons 'right (cons 2 (cons 1 empty)))) 3) (check-expect (robot-steps (cons 11 (cons 'right (cons 2 (cons 1 empty))))) 14) ; grow-animals : list-of-animals -> list-of-animals ; (define (grow-animals aloa) (cond [(empty? aloa) empty] [(cons? aloa) (cons (grow (first aloa)) (grow-animals (rest aloa))) ])) #;(define (grow-animals-bad-bad-bad aloa) (cond [(empty? aloa) empty] [(cons? aloa) (cons (if (boa? (first aloa)) (boa-name (first aloa))...(boa-num-spots (first aloa)... (dillo-length (first aloa))...(dillo-name (first aloa))...)) (grow-animals-bad-bad-bad (rest aloa))) ])) (check-expect (grow-animals empty) empty) (check-expect (grow-animals (cons (make-dillo "amy" 0.5 true) empty)) (cons (grow (make-dillo "amy" 0.5 true)) empty) )