;; traffic light exercise ; http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-9.html#node_sec_6.2 ;; dimensions of traffic light (define WIDTH 50) (define HEIGHT 160) (define BULB-RADIUS 20) (define BULB-DISTANCE 10) ;; the positions of the bulbs (define X-BULBS (quotient WIDTH 2)) (define Y-RED (+ BULB-DISTANCE BULB-RADIUS)) (define Y-YELLOW (+ Y-RED BULB-DISTANCE (* 2 BULB-RADIUS))) (define Y-GREEN (+ Y-YELLOW BULB-DISTANCE (* 2 BULB-RADIUS))) ;; draw the light with the red bulb turned on (start WIDTH HEIGHT) (draw-solid-disk (make-posn X-BULBS Y-RED) BULB-RADIUS 'red) (draw-circle (make-posn X-BULBS Y-YELLOW) BULB-RADIUS 'yellow) (draw-circle (make-posn X-BULBS Y-GREEN) BULB-RADIUS 'green) ;; clear the light bulb of a specified color ;; Contract: symbol -> boolean ;; Purpose: takes a symbol representing one of the ;; three traffic light colors (red, green, and yellow) ;; and clears the corresponding light bulb ;; Returns true if the operation is successful ;; Examples: (clear-bulb 'red) replaces the red bulb drawing ;; with a red circle (define (clear-bulb color) (cond [(symbol=? color 'red) (and (clear-solid-disk (make-posn X-BULBS Y-RED) BULB-RADIUS) (draw-circle (make-posn X-BULBS Y-RED) BULB-RADIUS 'red) )] ;; fill in the rest of the function code ) ) (check-expect (clear-bulb 'red) true) ;; Contract: symbol -> boolean ;; Purpose: takes a symbol representing one of the ;; three traffic light colors (red, green, and yellow) ;; and draws the corresponding light bulb ;; Returns true if the operation is successful ;; Examples: (draw-bulb 'red) replaces the red bulb drawing ;; with a red disk ;(draw-bulb 'green) ;; write a function (switch from to) as defined in exercise 6.2.4 ;(sleep-for-a-while 1) ;(switch 'green 'yellow) ;; use the function and the next function as stated in exercise 6.2.5 ;; inlcude the pause by using sleep-for-a-while function ;; next : symbol -> symbol ;; to switch a traffic light's current color and to return the next one ;(sleep-for-a-while 1) ; ;(next 'red) ;(sleep-for-a-while 1) ;(next 'green) ;(sleep-for-a-while 1) ;(next 'yellow) ;(sleep-for-a-while 1) ;(next 'red)