;; 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.1) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (require installed-teachpacks/picturing-programs) ; Exercise 17.1.1 ; Handlers: tick and draw ; Model is a string -- specifically either "red", "yellow", or "green" #| (define (function-on-light-color c) ; c light-color (cond [(string=? c "red") ...] [(string=? c "yellow") ...] [(string=? c "green") ...])) (define (function-returning-light-color ...) (cond [... "red"] [... "yellow"] [... "green"])) |# ; Tick handler ; change-light : light-color -> light-color (check-expect (change-light "green") "yellow") (check-expect (change-light "yellow") "red") (check-expect (change-light "red") "green") (define (change-light color) (cond [(string=? color "green") "yellow"] [(string=? color "yellow") "red"] [(string=? color "red") "green"])) ; Draw handler ; show-light : light-color -> image (check-expect (show-light "green") (circle 30 "solid" "green")) (check-expect (show-light "yellow") (circle 30 "solid" "yellow")) (check-expect (show-light "red") (circle 30 "solid" "red")) (define (show-light color) (circle 30 "solid" color)) (big-bang "green" (check-with string?) (on-tick change-light 5) (on-draw show-light))