PasteRack.org
Paste # 3487
2018-04-12 05:33:04

Fork as a new paste.

Paste viewed 44 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.    (- time 1))
  31.  
  32. ; the-end function
  33. (define (the-end time)
  34.   (< time 1))
  35.  
  36. ; width of the scene
  37. (define WIDTH 100)
  38.  
  39. ; height of the scene
  40. (define HEIGHT 100)
  41.  
  42. ; scene itself
  43. (define traff-light-scene
  44.   (empty-scene WIDTH HEIGHT))
  45.  
  46. ; circle radius
  47. (define RADIUS 50)
  48.  
  49. ; traffic light
  50. (define (traffic-light-render color)
  51.   (place-image
  52.    (circle RADIUS "solid"
  53.            (cond
  54.     [(modulo 2 color) "green"]
  55.     [(modulo 3 color) "yellow"]
  56.     [else "red"]))
  57.    50 50
  58.    traff-light-scene))
  59.  
  60. ; traffic-light-next program
  61. (define (traff-light-next time)
  62.   (big-bang time
  63.             (on-tick tock)
  64.             (to-draw traffic-light-render)
  65.             (stop-when the-end)
  66.             ))
  67.  
  68. ; for testing
  69. (traff-light-next 200)

=>

"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