;; 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 17.1.7) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require picturing-programs) ; Worked Exercise 17.1.7 #| ; A shape-color is one of the strings "red", "green", or "blue". (check-expect (function-on-shape-color "red") ...) (check-expect (function-on-shape-color "green") ...) (check-expect (function-on-shape-color "blue") ...) (define (function-on-shape-color c) (cond [(string=? c "red") ...] [(string=? c "green") ...] [(string=? c "blue") ...] )) (check-expect (function-returning-shape-color ...) "red") (check-expect (function-returning-shape-color ...) "green") (check-expect (function-returning-shape-color ...) "blue") (define (function-returning-shape-color ...) (cond [... "red"] [... "green"] [... "blue"] )) |# ; shape-color? : anything -> boolean (check-expect (shape-color? "red") true) (check-expect (shape-color? "green") true) (check-expect (shape-color? "blue") true) (check-expect (shape-color? "purple") false) (check-expect (shape-color? "beluga") false) (check-expect (shape-color? 17) false) (define (shape-color? thing) (and (string? thing) (or (string=? thing "red") (string=? thing "green") (string=? thing "blue")))) ; model is a shape-color ; draw handler show-triangle : shape-color -> image (check-expect (show-triangle "red") (triangle 30 "solid" "red")) (check-expect (show-triangle "green") (triangle 30 "solid" "green")) (check-expect (show-triangle "blue") (triangle 30 "solid" "blue")) (define (show-triangle c) (triangle 30 "solid" c)) ; tick handler next-color : shape-color -> shape-color (check-expect (next-color "red") "green") (check-expect (next-color "green") "blue") (check-expect (next-color "blue") (stop-with "blue")) (define (next-color c) ; c shape-color (cond [(string=? c "red") "green"] [(string=? c "green") "blue"] [(string=? c "blue") (stop-with "blue")] )) (big-bang "red" (check-with shape-color?) (on-draw show-triangle) (on-tick next-color 2))