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)
         (on-key go-left)
         (to-draw draw-bar))
    
    1. What is the first “thing” given to the draw handler? What kind of “thing” is it? (a.k.a. What is the type of the “initial model”?)
    2. What is the signature of the mouse handler go-right (not the original)?
    3. What is the signature of the key handler go-left (not the original)?
  1. The animation below is supposed to be a stick figure moving to the right in an empty scene. What is wrong with the code below? How would you fix it?

     (define (bad-push-right n) 
       (rectangle n 0 "solid" "orange"))
     (define (bad-draw n)
       (overlay (beside (rectangle n 0 "solid" "blue")
                        pic:stick-figure)
                 (empty-scene 300 200)))
     (big-bang 50
       (on-tick bad-push-right 0.25)
       (to-draw bad-draw))
    
Last modified October 18, 2021: wy-cs site update 2021-10-18 12:58 (c6b841a)