[plt-scheme] HTDP exercise

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Thu Sep 29 11:48:29 EDT 2005

On Sep 25, 2005, at 6:05 AM, arnuld wrote:

> Hi There,
>          I have developed a FUNCTION named
> "move-rect", which moves a rectangle across the
> canvas. The function consumes a number (delta) and a
> rectangle (a-rect). The function draws-and-clears the
> rectangle and returns a new rectangle that has been
> translated along x-axis by delta pixels. I have used
> this function to move "a-rect" ONE TIME only.

According to your contract your function produces a rect. How would you 
move this new rect?

-- Matthias


>
> PROBLEM: Can anybody tell me HOW TO Use this function
> to move the rectagle several times
>     (say  4) across the canvas?
>
> THANKS in ADVANCE
>
> SINCE it is written in "DRSCHEME programming
> environment" please use "draw.ss" TEACHPACK.
>
> (PLEASE do not get ANGRY, I am a 100% NEWBIE)
>
> ################ SOURCE-CODE ##################
>
> (define-struct rect (corner width height colour))
> ;; DATA-DEFINITION:
> ;; rect is a structure where c=posn, w,h=numbers &
> c=symbol
> ;; TEMPLATE : fun-for-rect : rect -> ???
> ;; (define (fun-for-rect a-rect)
> ;;     ..(rect-corner a-rect)...
> ;;      ..(rect-width a-rect)...
> ;;      ..(rect-height a-rect)...
> ;;     ..(rect-colour a-rect)...)
>
> ;; CONTRACT, PURPOSE & HEADER:
> ;; move-rect : number, rect -> rect
> ;; to move the given rectangle across the canvas by
> delta pixels
>
> (define a-rect (make-rect (make-posn 50 100) 40 20
> 'red))
> (start 500 300)
>
>
> (define (move-rect delta a-rect)
>   (cond
>     [(draw-and-clear-rect a-rect) (translate-rect
> delta a-rect)]
>     [else a-rect]))
>
>
> (define (translate-rect delta a-rect)
>   (draw-a-rect (make-rect (make-posn (+ (posn-x
> (rect-corner a-rect)) delta)
>                                            (posn-y
> (rect-corner a-rect)))
>                           (rect-width a-rect)
>                           (rect-height a-rect)
>                           (rect-colour a-rect))))
>
>
> (define (draw-and-clear-rect a-rect)
>   (cond
>     [(and (draw-a-rect a-rect) (sleep-for-a-while 2))
> (clear-a-rect a-rect)]
>     [else a-rect]))
>
>
> (define (draw-a-rect a-rect)
>   (draw-solid-rect (rect-corner a-rect)
>                    (rect-width a-rect)
>                    (rect-height a-rect)
>                    (rect-colour a-rect)))
>
> (define (clear-a-rect a-rect)
>   (clear-solid-rect (rect-corner a-rect)
>                     (rect-width a-rect)
>                     (rect-height a-rect)
>                     (rect-colour a-rect)))
>
>
> ;; test
> (move-rect 70 a-rect)
>
> ################ E. O. PROGRMME ###################
>
>
> THANKS
>
>
>       "arnuld"
>
>
>
> 		
> __________________________________________________________
> Yahoo! India Matrimony: Find your partner now. Go to 
> http://yahoo.shaadi.com
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.