[plt-scheme] Re: Permutation correct way to do it ?

From: Veer (diggerrrrr at gmail.com)
Date: Sun Apr 13 12:30:18 EDT 2008

Sorry to bother you too soon , but i have hit the wall again .

In chapter iv exercise 21.4 of online version of the book , i have to
replace draw-a-circle , clear-a-circle , draw-a-rectangle ,
clear-a-rectangle with process-circle and process-rectangle.
Also to replace draw-shape and clear-shape with process-shape .

I end up using the same no of functions after abstraction as they were
before , which defeats the purpose of abstraction. The problem i feel
is in process-shape and draw-losh  functions but cannot figure out new
design for them.


> Minor warning: In an early printing of HtDP, the ordering of this
> example was messed up. If so, it's a fantastic exercise to switch it
> around. Re-discovering typos is when you get close to mastery.
>

At present i am reading online version , but i am planning to buy the
book. I did send some
typos at authors at htdp.org


I have left translate related function from this code. This Code only
handles draw-losh and clear-losh .

Code:

;;mov-pic abstract2

(define-struct circle (center radius color))
;;A circle is a structure: (make-circle pos r col) where pos is posn structure ,
;;r is a number  and col is the symbol
;;eg (make-circle (make-posn 50 50) 40 'red)

(define-struct rectangle (uleft width height color))
;;A rectangle is a structure :
;;(make-rectangle ul w h c) where ul is posn structure , and w,h are
numbers , and c is symbol
;;eg (make-rectangle (make-posn 30 20) 5 5 'blue)


;;process-circle : (posn number symbol -> X)  circle -> X
;;to apply function f on the fields of a-circle
(define (process-circle f a-circle)
  (f (circle-center a-circle) (circle-radius a-circle) (circle-color a-circle)))

;;process-rectangle : (posn number number symbol ->X) rectangle -> X
;;to apply function f on the fields of  a-rect
(define (process-rectangle f a-rect)
  (f (rectangle-uleft a-rect) (rectangle-width a-rect) (rectangle-height a-rect)
     (rectangle-color a-rect)))


;;A shape is either
;;a circle or
;;a rectangle



;;process-shape : (???->X) shape -> X
;;to process a-shape ?
(define (process-shape f a-shape)
  (cond
    [(circle? a-shape) (process-circle f a-shape)]
    [(rectangle? a-shape) (process-rectangle f a-shape)]))



;;a list-of-shapes is either
;;empty or
;;(cons s losh) where s is a shape and losh is list-of-shapes

;;draw-losh : list-of-shapes -> boolean
;;to draw all the shapes in the list-of-shapes(alosh) on the canvas
(define (draw-losh alosh)
  (local ( (define (draw-shape a-shape)
             (cond
               [(circle? a-shape) (process-shape draw-circle a-shape)]
               [(rectangle? a-shape) (process-shape draw-solid-rect a-shape)]))
           (define (istrue? v)
             (and v true))
           )
      (andmap istrue? (map draw-shape alosh))))

;;clear-losh : list-of-shapes -> boolean
;;to clear all the  shapes in a list-of-shapes(alosh) on the canvas
(define (clear-losh alosh)
  (local ( (define (clear-shape a-shape)
             (cond
               [(circle? a-shape) (process-shape clear-circle a-shape)]
               [(rectangle? a-shape) (process-shape clear-solid-rect a-shape)]))
           (define (istrue? v)
             (and v true))
           )
    (andmap istrue? (map clear-shape alosh))))




;;FACE
(define FACE (cons (make-circle (make-posn 50 50) 40 'red)
                   (cons (make-rectangle (make-posn 30 20) 5 5 'blue)
                         (cons (make-rectangle (make-posn 65 20) 5 5 'blue)
                               (cons (make-rectangle (make-posn 40 75)
20 10 'red)
                                     (cons (make-rectangle (make-posn
45 35) 10 30 'blue) empty))))))

;;visual
(start 200 200)
(draw-losh FACE)
(sleep-for-a-while 1)
(clear-losh FACE)


Code Ends

Thanks
Veer



On 4/7/08, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>
> On Apr 7, 2008, at 8:35 AM, richard gere wrote:
> > After following sort example very carefully line by line word by word
>
> Minor warning: In an early printing of HtDP, the ordering of this
> example was messed up. If so, it's a fantastic exercise to switch it
> around. Re-discovering typos is when you get close to mastery.
>


Posted on the users mailing list.