PasteRack.org
Paste # 10678
2018-04-12 06:00:49

Fork as a new paste.

Paste viewed 56 times.


Embed:

  1. #lang racket
  2. "Exercise 50. If you copy and paste the
  3. above function definition into the definitions
  4. area of DrRacket and click RUN, DrRacket
  5. highlights two of the three cond lines.
  6. This coloring tells you that your test cases do
  7. not cover the full cond itional. Add enough
  8. tests to make DrRacket happy."
  9.  
  10. ; TrafficLight -> TrafficLight
  11. ; yields the next state given current state s
  12. (check-expect (traffic-light-next "red") "green")
  13. (check-expect (traffic-light-next "green") "yellow")
  14. (check-expect (traffic-light-next "yellow") "red")
  15. (define (traffic-light-next s)
  16.   (cond
  17.     [(string=? "red" s) "green"]
  18.     [(string=? "green" s) "yellow"]
  19.     [(string=? "yellow" s) "red"]))
  20.  
  21. "Exercise 51. Design a big-bang program
  22.  that simulates a traffic light for a
  23. given duration. The program renders the state of
  24. a traffic light as a solid circle of
  25. the appropriate color, and it changes state
  26. on every clock tick."
  27.  
  28. ; timer
  29. (define (tock time)
  30.    (cond
  31.     [(string=? "red" time) "green"]
  32.     [(string=? "green" time) "yellow"]
  33.     [(string=? "yellow" time) "red"]))
  34.  
  35. ; the-end function
  36. (define (the-end time)
  37.   (< time 1))
  38.  
  39. ; width of the scene
  40. (define WIDTH 100)
  41.  
  42. ; height of the scene
  43. (define HEIGHT 100)
  44.  
  45. ; scene itself
  46. (define traff-light-scene
  47.   (empty-scene WIDTH HEIGHT))
  48.  
  49. ; circle radius
  50. (define RADIUS 50)
  51.  
  52. ; traffic light
  53. (define (traffic-light-render color)
  54.   (place-image
  55.    (circle RADIUS "solid"
  56.            (cond
  57.     [(string=? "red" color) "green"]
  58.     [(string=? "green" color) "yellow"]
  59.     [(string=? "yellow" color) "red"]))
  60.    50 50
  61.    traff-light-scene))
  62.  
  63. ; traffic-light-next program
  64. (define (traff-light-next time)
  65.   (big-bang time
  66.             (on-tick tock)
  67.             (to-draw traffic-light-render)
  68.            ; (stop-when the-end)
  69.             ))
  70.  
  71. ; for testing
  72. (traff-light-next "red")
  73. "This program works good but need stop function."

=>

"Exercise 50. If you copy and paste the \r\nabove function definition into the definitions \r\narea of DrRacket and click RUN, DrRacket \r\nhighlights two of the three cond lines. \r\nThis coloring tells you that your test cases do \r\nnot cover the full cond itional. Add enough \r\ntests to make DrRacket happy."

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

"Exercise 51. Design a big-bang program\r\n that simulates a traffic light for a \r\ngiven duration. The program renders the state of \r\na traffic light as a solid circle of \r\nthe appropriate color, and it changes state \r\non every clock tick."

empty-scene: undefined;

 cannot reference an identifier before its definition

  in module: 'm

big-bang: undefined;

 cannot reference an identifier before its definition

  in module: 'm

"This program works good but need stop function."