Session #22
Answers
A world program represents a ball moving horizontally across the screen. The only changing information is the ball's x-coordinate. Define a data definition named BallWorld for the world state, include an interpretation, and write the basic template for a function that consumes a BallWorld.
; HorizontalBall
; interp represents the horizontal x position of the ball
(define (fn-for-ball hb) (... hb))
; HorizontalBall ; interp represents the horizontal x position of the ball (define (fn-for-ball hb) (... hb))
; BallWorld is Number
; interp. the x-coordinate of the ball
#;
(define (fn-for-ball-world bw)
(... bw))
; BallWorld is Number ; interp. the x-coordinate of the ball #; (define (fn-for-ball-world bw) (... bw))
Write a big-bang expression for a world program that starts at 0 and uses tick as its on-tick handler, draw as its to-draw handler, and handle-key as its on-key handler.
(define (main 0)
(on-tick tick)
(to-draw draw)
(on-key handle-key)))
```racket (define (main 0) (on-tick tick) (to-draw draw) (on-key handle-key))) ```
(big-bang 0
(on-tick tick)
(to-draw draw)
(on-key handle-key))
(big-bang 0 (on-tick tick) (to-draw draw) (on-key handle-key))
Write the signatures and stubs for three handlers: tick updates a WorldState every clock tick, draw renders a WorldState as an Image, and handle-key consumes a WorldState and KeyEvent and produces a WorldState.
; WorldState -> WorldState
; interp. update the world state data every tick
(define (tick ws) (... ws))
; WorldState -> Image
; interp. returns an image representation of the world state
(define (draw ws) (empty))
; WorldState, KeyEvent -> WorldState
; interp. takes a world state and key event and returns an updated world state
(define (handle-key ws ke)
(cond [q a]
[q a]))
; WorldState -> WorldState
; interp. update the world state data every tick
(define (tick ws) (... ws))
; WorldState -> Image
; interp. returns an image representation of the world state
(define (draw ws) (empty))
; WorldState, KeyEvent -> WorldState
; interp. takes a world state and key event and returns an updated world state
(define (handle-key ws ke)
(cond [q a]
[q a]))
; WorldState -> WorldState
(define (tick ws) ws)
; WorldState -> Image
(define (draw ws) empty-image)
; WorldState KeyEvent -> WorldState
(define (handle-key ws ke) ws)
; WorldState -> WorldState (define (tick ws) ws) ; WorldState -> Image (define (draw ws) empty-image) ; WorldState KeyEvent -> WorldState (define (handle-key ws ke) ws)
A key handler should reset the world to 0 when the space key is pressed and otherwise leave the world unchanged. Write the handler and two check-expect tests: one for the space key and one representative unrelated key.
(define (handle-key ws ke)
(cond [(key=? " ") 0]
[(else ws)]))
(check-expect (handle-key 42 " ") 0)
(check-expect (handle-key 10 "a") 10)f
```racket
(define (handle-key ws ke)
(cond [(key=? " ") 0]
[(else ws)]))
(check-expect (handle-key 42 " ") 0)
(check-expect (handle-key 10 "a") 10)f
```
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 25 " ") 0)
(check-expect (handle-key 25 "a") 25)
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 25 " ") 0)
(check-expect (handle-key 25 "a") 25)
Explain the flow of information in a simple big-bang program after an on-tick event occurs. Describe what the tick handler receives and produces, what happens to that result, and how to-draw is involved.
the on tick receives the world state and should return a world state (usually the tick function will update the world state in some way). Then the world program will pass this world state to the to-draw function which should produce an image to draw on the screen.
the on tick receives the world state and should return a world state (usually the tick function will update the world state in some way). Then the world program will pass this world state to the to-draw function which should produce an image to draw on the screen.
The on-tick handler receives the current world state and produces a new world state. big-bang stores that returned value as the new current world state. The to-draw handler can then consume that latest world state and produce the Image displayed on the screen.
The on-tick handler receives the current world state and produces a new world state. big-bang stores that returned value as the new current world state. The to-draw handler can then consume that latest world state and produce the Image displayed on the screen.
Write the signatures for these three world handlers: (1) tick consumes a WorldState and produces the next WorldState; (2) draw consumes a WorldState and produces an Image; (3) handle-key consumes a WorldState and a KeyEvent and produces a WorldState. Pay attention to the parameter order for the key handler.
; WorldState -> WorldState
(define (tick ws) ws)
; WorldState -> Image
(define (draw ws) (text (to-string ws) 20 "blackf'))
; WorldState, KeyEvent -> WorldState
(define (handle-key ws ke) ws)
; WorldState -> WorldState (define (tick ws) ws) ; WorldState -> Image (define (draw ws) (text (to-string ws) 20 "blackf')) ; WorldState, KeyEvent -> WorldState (define (handle-key ws ke) ws)
; WorldState -> WorldState
; WorldState -> Image
; WorldState KeyEvent -> WorldState
; WorldState -> WorldState ; WorldState -> Image ; WorldState KeyEvent -> WorldState
You are designing a world program but have not implemented its handlers yet. Write callable stubs for tick, draw, and handle-key with the signatures WorldState -> WorldState, WorldState -> Image, and WorldState KeyEvent -> WorldState. Do not implement the real behavior yet.
(define (tick ws) ws)
(define (draw ws) (text (to-string ws) 20 "black"))
(define (handle-key ws ke) ws)
(define (tick ws) ws) (define (draw ws) (text (to-string ws) 20 "black")) (define (handle-key ws ke) ws)
; WorldState -> WorldState
(define (tick ws) ws)
; WorldState -> Image
(define (draw ws) empty-image)
; WorldState KeyEvent -> WorldState
(define (handle-key ws ke) ws)
; WorldState -> WorldState (define (tick ws) ws) ; WorldState -> Image (define (draw ws) empty-image) ; WorldState KeyEvent -> WorldState (define (handle-key ws ke) ws)
Write a complete key handler named handle-key that resets a numeric WorldState to 0 when the space key is pressed and otherwise returns the world unchanged. Then write two check-expect tests: starting from 20 with the space key should produce 0, and starting from 20 with the key "a" should produce 20.
(define (handle-key ws ke)
(cond [(key=? " ") 0]
[(else ws)]))
(check-expect (handle-key 20 " ") 0)
(check-expect (handle-key 20 "r") 20)
(define (handle-key ws ke)
(cond [(key=? " ") 0]
[(else ws)]))
(check-expect (handle-key 20 " ") 0)
(check-expect (handle-key 20 "r") 20)
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 20 " ") 0)
(check-expect (handle-key 20 "a") 20)
(define (handle-key ws ke)
(cond [(key=? ke " ") 0]
[else ws]))
(check-expect (handle-key 20 " ") 0)
(check-expect (handle-key 20 "a") 20)