;;;; This example illustrates the use of the design recipe for ;;;; functions consuming compound (structured) data. ;;;; This one is a little more complex than some because there ;;;; are actually 2 kinds of structured data involved (Circle ;;;; and Posn), and the right way to deal with this is to develop ;;;; an additional helper function. ;; (Step 1) Data definition (define-struct circle (center radius color)) ;; A Circle is (make-circle Posn Number Symbol) ;; (Step 1 continued) Circle examples (define c1 (make-circle (make-posn 3 4) 2 'red)) (define c2 (make-circle (make-posn 0 0) 5 'black)) ;; Note that we're taking for granted the following data definition: ;; A Posn is (make-posn Number Number) ;; (Step 2) Contract/purpose/header (for main function) ;; move-circle : Circle Number -> Circle ;; moves a circle horizontally by a given distance ;; (define (move-circle a-circle dist) ...) #| (Step 4) Template (for main function) (define (move-circle a-circle dist) ... (circle-center a-circle) ... ... (circle-radius a-circle) ... ... (circle-color a-circle) ... ) |# ;; (Step 5) Body (of main function) (define (move-circle a-circle dist) (make-circle (move-posn (circle-center a-circle) dist) (circle-radius a-circle) (circle-color a-circle))) ;; (Step 2) Contract/purpose/header (for helper) ;; move-posn : Posn Number -> Posn ;; moves a posn horizontally ;; (define (move-posn a-posn delta-x) ...) #| (Step 4) Template (for helper) (define (move-posn a-posn delta-x) ... (posn-x a-posn) ... ... (posn-y a-posn) ... ) |# ;; (Step 4) Body (for helper) (define (move-posn a-posn delta-x) (make-posn (+ (posn-x a-posn) delta-x) (posn-y a-posn))) ;; (Step 3) Examples and (Step 6) Tests (for helper) (move-posn (make-posn 3 4) 5) "should be" (make-posn 8 4) (move-posn (make-posn 0 0) -2) "should be" (make-posn -2 0) ;; (Step 3) Examples and (Step 4) Tests (for main function) (move-circle c1 -2) "should be" (make-circle (make-posn 1 4) 2 'red) (move-circle c2 10) "should be" (make-circle (make-posn 10 0) 5 'black)