big-bang 00

Why do handlers have specific signatures?

Application Practice

  1. You want to turn pah into a key handler pa. Write the signature for pa. Write the new function pa.

     (define pusher-square (square 5 "solid" "transparent"))
     (define (pah x) (beside pusher-square x))
    
  2. You need to modify the definition of your mouse handler pm so that it can be run when you press a key instead of using the mouse. How do you do that?

     (define (pm img x y event) (rotate 5 img))
    
  3. What kind of handler is the function pq appropriate for right now?

     (define (pq a b c d) (crop-left a 5))
    
  4. Convert the pq above to a tick handler pqt. Also give the correct signature of pqt.

  5. Use the starter code below to answer the questions.

     (define (go-right n) (+ n 5))
     (define (go-left n) (- n 5))
     (define (draw-bar n) (rectangle n 30 "solid" "purple"))
     (big-bang 100
         (on-mouse go-right-m)
         (on-key go-left-k)
         (to-draw draw-bar))
    
    1. What is the first “thing” given to the draw handler? What kind of “thing” is it? (That is, what is the type of the “initial model”?)
    2. Edit the code to change the function go-right into a mouse handler go-right-m. What is the signature of go-right-m?
    3. Edit the code to change the function go-left into a key handler. What is the signature of the key handler go-left-k?
  1. The animation below is supposed to be a stick figure moving to the right in an empty scene. The code has problems.

    1. Illustrate the problem by drawing a “three column model/drawing diagram” beginning with the initial model.
    2. What is wrong with the code below?
    3. How would you fix it?

    The code:

     (define (bad-push-right n)
       (rectangle n 1 "solid" "orange"))
     (define (bad-draw n)
       (overlay (beside (rectangle n 1 "solid" "blue")
                        pic:stick-figure)
                 (empty-scene 300 200)))
     (big-bang 50
       (on-tick bad-push-right 0.25)
       (to-draw bad-draw))