Code Outline (Functional Version), part 2
;;; weight : Shape -> Number ;;; GIVEN: a shape ;;; RETURNS: the weight of the shape, assuming each shape ;;; weighs 1 gram per pixel of area ;;; STRATEGY: use template for Shape on s (define (weight s) (cond ((my-circle? s) (my-circle-weight s)) ((my-square? s) (my-square-weight s)) ((my-composite? s) (my-composite-weight s)))) ;;; add-to-scene : Shape Scene -> Scene ;;; RETURNS: a scene like the given one, but with the ;;; given shape painted on it ;;; STRATEGY: use template for Shape on s (define (add-to-scene s scene) (cond ((my-circle? s) (my-circle-add-to-scene s scene)) ((my-square? s) (my-square-add-to-scene s scene)) ((my-composite? s) (my-composite-add-to-scene s scene))))
There are six small help functions left to write:
-
my-circle-weight
-
my-square-weight
-
my-composite-weight
-
my-circle-add-to-scene
-
my-square-add-to-scene
-
my-composite-add-to-scene