;; A Circle is a structure (make-circle (Posn Number)) (define-struct circle (center radius)) #| Template for Circles ;; f: Circle -> ?? (define (f c) ... (circle-center c) ... (circle-radius c)) |# ;; A Square is a structure (make-square (Posn Number)) (define-struct square (topleft side)) #| Template for Squares ;; f: Square -> ?? (define (f s) ... (square-topleft s) ... (square-side s)) |# ;; A Shape is one of ;; -- (make-circle Posn Number) ;; -- (make-square Posn Number) #| Tempalte for Shapes ;; f: Shape -> ?? (define (f sh) (cond [(circle? sh) (circle-center sh) ... (circle-radius sh) ...] [(square? sh) (square-topleft sh) ... (square-side sh) ... ])) |# ;; Areas: ;; Circle: pi * r * r ;; Square: s * s ;; area: Shape -> Number ;; Compute area of shape (define (area sh) (cond [(circle? sh) (* pi (circle-radius sh) (circle-radius sh)) ] [(square? sh) (* (square-side sh) (square-side sh)) ])) ;; Examples (= (area (make-circle (make-posn 0 0) 5)) (* pi 25)) (= (area (make-square (make-posn 0 0) 5)) 25) ;; Perimeter of shapes ;; Circle: 2 * pi * r ;; Square: 4 * side ;; perimeter: Shape -> Number ;; find perimeter (define (perimeter sh) (cond [(circle? sh) (* 2 pi (circle-radius sh))] [(square? sh) (* 4 (square-side sh)) ])) ;; Tests (= (perimeter (make-circle (make-posn 0 0) 5)) (* pi 10)) (= (perimeter (make-square (make-posn 0 0) 5)) 20) #| ;; f: Shape -> ?? (define (f sh) (cond [(circle? sh) (f-circle sh)] [(square? sh) (f-square sh)])) |# ;; area-circle: Circle -> Number ;; area of circle c (define (area-circle c) (* pi (circle-radius c) (circle-radius c))) ;; area-square: Square -> Number ;; area of square (define (area-square s) (* (square-side s) (square-side s))) ;; area2: Shape -> Number ;; area of shape (define (area2 sh) (cond [(circle? sh) (area-circle sh)] [(square? sh) (area-square sh)])) ;; Examples (= (area2 (make-circle (make-posn 0 0) 5)) (* pi 25)) (= (area2 (make-square (make-posn 0 0) 5)) 25)