Exercise 6.2.2: (back)

;; dimensions of traffic light
(define WIDTH 50)
(define HEIGHT 160)
(define BULB-RADIUS 20)
(define BULB-DISTANCE 10)


;; the positions of the bulb
(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)))


;; clear the bulb: symbol -> symbol
(define (clear-bulb bulb)
  (cond
    [(symbol=? bulb 'red) (and (hide-disk bulb) 
                               (show-circle bulb))]
    [(symbol=? bulb 'yellow) (and (hide-disk bulb)
                               (show-circle bulb))]
    [(symbol=? bulb 'green) (and (hide-disk bulb)
                               (show-circle bulb))]))

                               
;; hide-disk: symbol -> symbol
(define (hide-disk bulb)
  (cond
    [(symbol=? bulb 'red) 
     (clear-solid-disk (make-posn X-BULBS Y-RED) BULB-RADIUS 'red)]
    [(symbol=? bulb 'yellow) 
     (clear-solid-disk (make-posn X-BULBS Y-YELLOW) BULB-RADIUS 'yellow)]
    [(symbol=? bulb 'green)
     (clear-solid-disk (make-posn X-BULBS Y-GREEN) BULB-RADIUS 'green)]))


;; show-circle: symbol -> symbol
(define (show-circle bulb)
  (cond
    [(symbol=? bulb 'red)
     (draw-circle (make-posn X-BULBS Y-RED) BULB-RADIUS 'red)]
    [(symbol=? bulb 'yellow)
     (draw-circle (make-posn X-BULBS Y-YELLOW) BULB-RADIUS 'yellow)]
    [(symbol=? bulb 'green)
     (draw-circle (make-posn X-BULBS Y-GREEN) BULB-RADIUS 'green)]))


;; 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)