Example: Design recipe for self-referential data

We are using the earlier definitions of Circle and Rectangle, and we leave it to the reader to design the helper functions (c-within c p) and (r-within r p) that determine whether the Posn p is within the Circle c or Rectangle r respectively.

;; A Combo is a structure of Shapes Shapes
(define-struct combo (top bottom))

;; A Shapes is either
;;  - a Circle
;;  - a Rectangle
;;  - a Combo

;; Examples of Shapes
;; remember earlier definitions of cir1, cir2, rect1,and rect2

(define combo1 (make-combo cir1 cir2))
(define combo2 (make-combo rect1 combo1))
(define combo3 (make-combo combo2 rect2))


;; determine whether p is within the shapes cs
;; Shapes -> Boolean
;; (define (within? cs p) ...

;; Examples (to be used as tests after the program is written)
;; (equal? (within? combo1 (make-posn 30 10)) true)
;; (equal? (within? combo1 (make-posn 30 50)) false)
;; (equal? (within? combo3 (make-posn 50 25)) true)
;; (equal? (within? combo2 (make-posn 50 25)) false)

;; Template:
;;  (cond
;;    [(circle? cs) ...]
;;    [(rect? cs)   ...]
;;    [else ... (within? (combo-top cs) p) ...
;;          ... (within? (combo-bottom cs) ...]

;; Program:
(define (within? cs p)
  (cond
   [(circle? cs) (c-within? cs p)]
   [(rect? cs)   (r-within? cs p)]
   [else (or (within? (combo-top cs) p)
             (within? (combo-bottom cs) p))]))