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

From: Veer (diggerrrrr at gmail.com)
Date: Wed Apr 16 03:06:37 EDT 2008

What can i say , brilliant! , you make it look so easy .There was
absolutely no way i could have come up with this ->  (process-shape
a-shape draw-circle draw-solid-rect) .

 For the abstraction of draw-shape and clear-shape , I saw  that both
of them operate on a-shape hence i thought i can just use "f a-shape"
inside the (process-shape f a-shape)  followed by the wrong use of
step 3 ,all this lead to disaster.

Here is another attempt for process-shape , translate-shape ,
draw-losh , clear-losh and
translate-losh also now i can write a proper contract for process-shape.
Now if i add another drawing object to it , i will need to only edit
process-shape ,translate-shape,draw-shape and clear-shape.

;;process-shape : (posn number symbol -> X) (posn number number symbol
->X) shape -> X
;;to process a-shape by passing f or g to it .
(define (process-shape f g a-shape)
  (cond
    [(circle? a-shape) (process-circle f a-shape)]
    [(rectangle? a-shape) (process-rectangle g a-shape)]))

;;translate-shape : number shape  -> shape
;;to add delta amount to  a-shape  to its x position
(define (translate-shape delta a-shape)
  (process-shape (lambda (pos r c)
                         (make-circle (make-posn (+ (posn-x pos)
delta) (posn-y pos)) r c))
                 (lambda (pos w h c)
                   (make-rectangle (make-posn (+ (posn-x pos) delta)
(posn-y pos)) w h c))
                 a-shape))



;;draw-shape : shape -> boolean
;;to draw a-shape on the canvas
(define (draw-shape a-shape)
  (process-shape draw-circle draw-solid-rect a-shape))


;;clear-shape : shape -> boolean
;;to clear  a-shape on the canvas
(define (clear-shape a-shape)
  (process-shape clear-circle clear-solid-rect a-shape))

;;draw-losh : (listof shape) -> boolean
;;to draw a listof shape (a-losh) on the canvas
(define (draw-losh a-losh)
  (not (andmap not (map draw-shape a-losh))))

;;clear-losh: (listof shape) -> boolean
;;to clear a listof shape (a-losh) on the canvas
(define (clear-losh a-losh)
  (not (andmap not (map clear-shape a-losh))))

;;translate-losh : delta (listof shape) -> (listof shape)
;;to add delta amount to all the shapes in a-losh to there x positions
(define (translate-losh delta alosh)
  (map (lambda (a-shape) (translate-shape delta a-shape) ) alosh))



Thanks
Veer


Posted on the users mailing list.