;;;; This is an example of the use of the design recipe for ;;;; structured data when the data is defined to be the union ;;;; ("or") of more basic structured types. ;; (Step 1) Data Definition (define-struct circle (center radius color)) (define-struct square (center size color)) ;; A Shape is one of ;; - (make-circle Posn Number Symbol) ;; - (make-square Posn Number Symbol) ;; (Step 1 continued) Shape examples (define circle1 (make-circle (make-posn 1 2) 3 'red)) (define square1 (make-square (make-posn 4 5) 1 'blue)) ;; (Step 2) Contract/purpose/header ;; move-shape : Shape Number -> Shape ;; moves a shape horizontally by a given distance ;; (define (move-shape a-shape dist) ...) #| (Step 4) Template (define (move-shape a-shape dist) (cond [(circle? a-shape) ... (circle-center a-shape) ... ... (circle-radius a-shape) ... ... (circle-color a-shape) ... ] [(square? a-shape) ... (square-center a-shape) ... ... (square-size a-shape) ... ... (square-color a-shape) ... ])) |# ;; (Step 5) Body (define (move-shape a-shape dist) (cond [(circle? a-shape) (make-circle (move-posn (circle-center a-shape) dist) (circle-radius a-shape) (circle-color a-shape))] [(square? a-shape) (make-square (move-posn (square-center a-shape) dist) (square-size a-shape) (square-color a-shape))])) #| We'll take for granted that we've developed and tested this helper before, so here we'll omit its template, examples, and tests. |# ;; move-posn : Posn Number -> Posn ;; moves given posn horizontally by the given amount (define (move-posn a-posn d) (make-posn (+ (posn-x a-posn) d) (posn-y a-posn))) ;; (Step 3) Examples and (Step 6) Tests (move-shape circle1 10) "should be" (make-circle (make-posn 11 2) 3 'red) (move-shape square1 -4) "should be" (make-square (make-posn 0 5) 1 'blue)