Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 2.88 KB | None | 0 0
  1. ;; A simple animated traffic light.
  2.  
  3. ;; =================
  4. ;; Constants:
  5.  
  6. (define RADIUS 20) ; of each light
  7. (define SPACING 6) ; space between and beside lights
  8.  
  9. (define BACKGROUND (rectangle (+ (* 2 SPACING) (* 2 RADIUS))
  10.                               (+ (* 4 SPACING) (* 6 RADIUS))
  11.                               "solid"
  12.                               "black"))
  13.  
  14. (define SPACE (square SPACING "solid" "black"))
  15.  
  16. (define RON
  17.   (overlay (above SPACE
  18.                   (circle RADIUS "solid"   "red")
  19.                   SPACE
  20.                   (circle RADIUS "outline" "yellow")
  21.                   SPACE
  22.                   (circle RADIUS "outline" "green")
  23.                   SPACE)
  24.            BACKGROUND))
  25.  
  26. (define YON
  27.   (overlay (above SPACE
  28.                   (circle RADIUS "outline" "red")
  29.                   SPACE
  30.                   (circle RADIUS "solid"   "yellow")
  31.                   SPACE
  32.                   (circle RADIUS "outline" "green")
  33.                   SPACE)
  34.            BACKGROUND))
  35.  
  36. (define GON
  37.   (overlay (above SPACE
  38.                   (circle RADIUS "outline" "red")
  39.                   SPACE
  40.                   (circle RADIUS "outline" "yellow")
  41.                   SPACE
  42.                   (circle RADIUS "solid"   "green")
  43.                   SPACE)
  44.            BACKGROUND))
  45.  
  46.  
  47.  
  48. ;; =================
  49. ;; Data definitions:
  50.  
  51. ;; Light is one of:
  52. ;;  - "red"
  53. ;;  - "yellow"
  54. ;;  - "green"
  55. ;; interp. the current color of the light
  56. ;; <examples are redundant for enumerations>
  57.  
  58. #;
  59. (define (fn-for-light l)
  60.   (cond [(string=? l "red")    (...)]
  61.         [(string=? l "yellow") (...)]
  62.         [(string=? l "green")  (...)]))
  63.  
  64. ;; Template rules used:
  65. ;;   one of: 3 cases
  66. ;;   atomic distinct: "red"
  67. ;;   atomic distinct: "yellow"
  68. ;;   atomic distinct: "green"
  69.  
  70.  
  71.  
  72. ;; =================
  73. ;; Functions:
  74.  
  75. ;; Light -> Light
  76. ;; called to run the animation; start with (main "red")
  77. ;; no tests for main function
  78. (define (main l)
  79.   (big-bang l
  80.             (on-tick next-color 1)   ; Light -> Light
  81.             (to-draw render-light))) ; Light -> Image
  82.  
  83.  
  84.  
  85.  
  86. ;; Light -> Light
  87. ;; produce next color of light
  88. (check-expect (next-color "red")    "green")
  89. (check-expect (next-color "yellow") "red")
  90. (check-expect (next-color "green")  "yellow")
  91.  
  92. #;
  93. (define (next-color l)      ; stub
  94.   "red")
  95. ;<template from Light>
  96.  
  97. (define (next-color l)
  98.   (cond [(string=? l "red")    "green"]
  99.         [(string=? l "yellow") "red"]
  100.         [(string=? l "green")  "yellow"]))
  101.  
  102.  
  103.  
  104. ;; Light -> Image
  105. ;; produce appropriate image for light color
  106. (check-expect (render-light "red")    RON)
  107. (check-expect (render-light "yellow") YON)
  108. (check-expect (render-light "green")  GON)
  109.  
  110. #;
  111. (define (render-light l)
  112.   BACKGROUND)
  113. ;<template from Light>
  114.  
  115. (define (render-light l)
  116.   (cond [(string=? l "red")    RON]
  117.         [(string=? l "yellow") YON]
  118.         [(string=? l "green")  GON]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement