[plt-scheme] Re: Permutation correct way to do it ?
Well i am clueless , i am not at all convinced that i am using right approach .
I don't know what to do with process-shape and draw-losh and
clear-losh . I again end up using draw-shape and clear-shape functions
inside the draw-losh and clear-losh . I just can't see how they can be
removed. I cannot do this (map process-shape list-of-shapes) , but
can do this
(map draw-a-shape list-of-shapes) where draw-a-shape call
process-shape with appropriate arguments. Sorry for confusing you ,
here is the code :
;;mov-pic abstraction3
(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 with fields of a-circle as its argument
(define (process-circle f a-circle)
(f (circle-center a-circle) (circle-radius a-circle) (circle-color a-circle)))
;;draw-a-circle : circle -> boolean
;;to draw a-circle on the canvas
(define (draw-a-circle a-circle)
(process-circle draw-circle a-circle))
;;clear-a-circle : circle -> boolean
;;to clear a-circle on the canvas
(define (clear-a-circle a-circle)
(process-circle clear-circle a-circle))
;;process-rectangle : (posn number number symbol ->X) rectangle -> X
;;to apply function f with fields of a-rect as its arguments
(define (process-rectangle f a-rect)
(f (rectangle-uleft a-rect) (rectangle-width a-rect) (rectangle-height a-rect)
(rectangle-color a-rect)))
;;draw-a-rectangle : rectangle->boolean
;;to draw a-rect on the canvas
(define (draw-a-rectangle a-rect)
(process-rectangle draw-solid-rect a-rect))
;;clear-a-rect : rectangle -> boolean
;;to clear a-rect on the canvas
(define (clear-a-rectangle a-rect)
(process-rectangle clear-solid-rect a-rect))
;;A shape is either
;;a circle or
;;a rectangle
;;process-shape : i don't know how to write a contract for this function
;;to apply f1 with f2 and a-shape as its arguments
;;f1 is either process-rectangle or process-circle
(define (process-shape f1 f2 a-shape)
(f1 f2 a-shape))
;;(define (process-shape2 f2 a-shape)
;; (cond
;; [(circle? a-shape) (process-circle f2 a-shape)]
;; [(rectangle? a-shape) (process-rectangle f2 a-shape)]))
;;draw-shape : shape -> boolean
;;to draw a-shape on the canvas
(define (draw-shape a-shape)
(cond
[(circle? a-shape) (process-shape process-circle draw-circle a-shape)]
[(rectangle? a-shape) (process-shape process-rectangle
draw-solid-rect a-shape)]))
;;clear-shape : shape -> boolean
;;to clear the shape on the canvas
(define (clear-shape a-shape)
(cond
[(circle? a-shape) (process-shape process-circle clear-circle a-shape)]
[(rectangle? a-shape) (process-shape process-rectangle
clear-solid-rect a-shape)]))
;;(start 200 200)
;;(draw-shape (make-circle (make-posn 50 50) 40 'red))
;;(draw-shape (make-rectangle (make-posn 30 20) 5 5 'blue))
;;(sleep-for-a-while 1)
;;(clear-shape (make-circle (make-posn 50 50) 40 'red))
;;(clear-shape (make-rectangle (make-posn 30 20) 5 5 'blue))
;;draw-losh : list-of-shapes -> (listof boolean)
;;to draw a list-of-shapes(alosh) on the canvas
;;
(define (draw-losh alosh)
(local ((define (draw-a-shape a-shape)
(cond
[(circle? a-shape) (process-shape process-circle
draw-circle a-shape)]
[(rectangle? a-shape) (process-shape process-rectangle
draw-solid-rect a-shape)]))
)
(map draw-a-shape alosh)) )
;;clear-losh : list-of-shapes -> (listof boolean)
;;to clear a list-of-shapes(alosh) on the canvas
(define (clear-losh alosh)
(local ((define (clear-a-shape a-shape)
(cond
[(circle? a-shape) (process-shape process-circle
clear-circle a-shape)]
[(rectangle? a-shape) (process-shape process-rectangle
clear-solid-rect a-shape)]))
)
(map clear-a-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))))))
(start 200 200)
(draw-losh FACE)
(sleep-for-a-while 1)
(clear-losh FACE)
Thanks
Veer
On 4/15/08, Matthias Felleisen <matthias at ccs.neu.edu> wrote:
>
> On Apr 13, 2008, at 12:30 PM, Veer wrote:
> > ;;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)))
>
>
> You didn't finish the design recipe here. Before you proceed,
> you must demonstrate that that you can define
>
> draw-circle in terms of process-circle
> clear-circle in terms of process-circle
>
> (and these definitions should be really short). Do so for all
> abstractions.
>
> ;; ---
>
> You can also abstract over the list-processing functions
> draw-losh, clear-losh but of course, the appropriate function
> already exists; see table on page 313.
>
> -- Matthias
>
>