-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-4-7-refactored.rkt~
52 lines (40 loc) · 1.58 KB
/
1-4-7-refactored.rkt~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
;; 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 1-4-7-refactored) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f)))
; A current state falls into one of three categories
; "green"
; "yellow"
; "red"
; interpretation the colors of a traffic light
; TrafficLight -> TrafficLight
; yields the next state, given current state cs
(define (tl-next cs)
(cond [(string=? cs "green") "yellow"]
[(string=? cs "yellow") "red"]
[else "green"]))
(require 2htdp/image)
; TrafficLight -> Image
; renders the current state cs as an image
(define (tl-render current-state)
(place-image (3-lights current-state) 45 15 (empty-scene 90 30)))
(define (green-light cs)
(if (string=? cs "green") (circle 15 "solid" "green")
(circle 15 "outline" "green")))
(define (yellow-light cs)
(if (string=? cs "yellow") (circle 15 "solid" "yellow")
(circle 15 "outline" "yellow")))
(define (red-light cs)
(if (string=? cs "red") (circle 15 "solid" "red")
(circle 15 "outline" "red")))
(define (3-lights cs)
(beside (green-light cs)
(yellow-light cs)
(red-light cs)))
(require 2htdp/universe)
; TrafficLight -> TrafficLight
; simulates a clock-based American traffic light
(define (traffic-light-simulation initial-state)
(big-bang initial-state
[to-draw tl-render]
[on-tick tl-next 1]))
(traffic-light-simulation "green")