PasteRack.org
Paste # 28500
2018-04-09 09:29:13

Fork as a new paste.

Paste viewed 58 times.


Embed:

Section3.6(exercise.with.auto)

  1. #lang racket
  2. "Sample Problem Design a program that moves a car from left to right on the world
  3. canvas, three pixels per clock tick."
  4.  
  5. ; width of the world canvas
  6. (define WIDTH-OF-WORLD 400)
  7.  
  8. ; heigh of the world canvas
  9. (define HEIGH-OF-WORLD 50)
  10.  
  11. ; canvas itself
  12. (define BACKGROUND
  13.   (empty-scene WIDTH-OF-WORLD HEIGH-OF-WORLD))
  14.  
  15. ; radius of car`s wheels
  16. (define WHEEL-RADIUS 5)
  17. (define WHEEL-DISTANCE (* WHEEL-RADIUS 5))
  18.  
  19. ; draw a wheel
  20. (define WHEEL
  21.   (circle WHEEL-RADIUS "solid" "black"))
  22.  
  23. ; distance between wheels
  24. (define SPACE
  25.   (rectangle WHEEL-DISTANCE WHEEL-RADIUS "solid" "gray"))
  26.  
  27. ; the car?
  28. (define BOTH-WHEELS
  29.   (beside WHEEL SPACE WHEEL))
  30.  
  31. ; A WorldState is a Number.
  32. ; interpretation the number of pixels between
  33. ; the left border of the scene and the car
  34. "An alternative is to count the number of clock ticks that have passed and to use this number
  35. as the state of the world. We leave this design variant as an exercise."
  36.  
  37. ; WorldState -> Image
  38. ; places the image of the car x pixels from
  39. ; the left margin of the BACKGROUND image
  40. (define (render1 num)
  41.   (place-image BOTH-WHEELS num 40 BACKGROUND))
  42. (check-expect (render1 50) (place-image BOTH-WHEELS 50 40 BACKGROUND))
  43. (check-expect (render1 100) (place-image BOTH-WHEELS 100 40 BACKGROUND))
  44. (check-expect (render1 150) (place-image BOTH-WHEELS 150 40 BACKGROUND))
  45. (check-expect (render1 200) (place-image BOTH-WHEELS 200 40 BACKGROUND))
  46.  
  47.  
  48. (define (render num)
  49.   (place-image BOTH-WHEELS num 40 ROAD))
  50.  
  51. ; WorldState -> WorldState
  52. ; adds 3 to x to move the car right
  53. (define (tock x)
  54.   (+ x 3))
  55. "Exercise 40. Formulate the examples as BSL tests, that is, using the check-expect form.
  56. Introduce a mistake. Re-run the tests."
  57. (check-expect (tock 0) 3)
  58. (check-expect (tock 8) 11)
  59. (check-expect (tock 6) 9)
  60. ;(check-expect (tock 7) 9); <- Introducing an error.
  61.  
  62. ; a tree to the BACKGROUND
  63. (define tree
  64.   (underlay/xy (circle 10 "solid" "green")
  65.                9 15
  66.                (rectangle 2 20 "solid" "brown")))
  67.  
  68. ; a tree1 to the BACKGROUND
  69. (define tree1
  70.   (underlay/xy (circle 11 "solid" "red")
  71.                9 15
  72.                (rectangle 2 20 "solid" "chocolate")))
  73.  
  74. ; draw a ROAD
  75. (define ROAD
  76.   (place-images
  77.    (list tree
  78.          tree1
  79.          tree1
  80.          tree
  81.          tree
  82.          tree1)
  83.    (list (make-posn 10 40)
  84.          (make-posn 18 40)
  85.          (make-posn 60 40)
  86.          (make-posn 70 40)
  87.          (make-posn 270 40)
  88.          (make-posn 280 40))
  89.    BACKGROUND))
  90.  
  91. ; function that stops a car
  92. (define (the-end y)
  93.   (> y 300))
  94.  
  95. ; WorldState - WorldState
  96. ; launches the program from some initial state
  97. (define (main ws)
  98.   (big-bang ws
  99.            {on-tick tock}
  100.            ;{passed-ticks ws}
  101.            {to-draw render}
  102.            ;{on-mouse hyper}
  103.            {stop-when the-end}))
  104.  
  105. ; for testing
  106. ROAD
  107. (main 5)

=>

"Sample Problem Design a program that moves a car from left to right on the world\r\ncanvas, three pixels per clock tick."

empty-scene: undefined;

 cannot reference an identifier before its definition

  in module: 'm

circle: undefined;

 cannot reference an identifier before its definition

  in module: 'm

rectangle: undefined;

 cannot reference an identifier before its definition

  in module: 'm

beside: undefined;

 cannot reference an identifier before its definition

  in module: 'm

"An alternative is to count the number of clock ticks that have passed and to use this number\r\nas the state of the world. We leave this design variant as an exercise."

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

check-expect: undefined;

 cannot reference an identifier before its definition

  in module: 'm

"Exercise 40. Formulate the examples as BSL tests, that is, using the check-expect form.\r\nIntroduce a mistake. Re-run the tests."

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

underlay/xy: undefined;

 cannot reference an identifier before its definition

  in module: 'm

underlay/xy: undefined;

 cannot reference an identifier before its definition

  in module: 'm

place-images: undefined;

 cannot reference an identifier before its definition

  in module: 'm

ROAD: undefined;

 cannot reference an identifier before its definition

  in module: 'm

big-bang: undefined;

 cannot reference an identifier before its definition

  in module: 'm