;; 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 world-of-traffic) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp") (lib "universe.rkt" "teachpack" "2htdp"))))) ;; DEMO: world of traffic ;;data definitions ;;A 'light' is either ;; - "red" ;; - "yellow" ;; - "green" ;; bulb : color -> image ;; to render a traffic light bulb of the given color (define (bulb color) (circle RADIUS "solid" color)) (define RADIUS 12) (define red-bulb (bulb "red")) (define dark-bulb (bulb "black")) (check-expect red-bulb (circle 12 "solid" "red")) (define spacer (square (/ RADIUS 4) 0 "white")) ;; light->image : light -> image ;; draw the light (define (light->image light) (cond [(string=? light "red") (above red-bulb spacer dark-bulb spacer dark-bulb)] [(string=? light "yellow") (above dark-bulb spacer (bulb "yellow") spacer dark-bulb)] [(string=? light "green") (above dark-bulb spacer dark-bulb spacer (bulb "green")) ])) ;; change-light : light -> light ;; given the current color, return the next color (define (change-light light) (cond [(string=? light "red") "green"] [(string=? light "yellow") "red"] [(string=? light "green") "yellow"])) (check-expect (change-light "green") "yellow") (check-expect (change-light "yellow") "red") (check-expect (change-light "red") "green") (check-error (change-light "purple")) ;; run-simulation : number -> world (define (run-simiulation rate) (big-bang "green" (on-tick change-light rate) (to-draw light->image)))