;; Contract and Purpose
;; Shape -> Number
;; compute the distance of a shape s from the origin
;; (define (distTo0 s) ...
;; Examples:
;; (= (distTo0 cir1) 30)
;; (= (distTo0 cir2) 50)
;; (= (distTo0 rect1) 30)
;; (= (distTo0 rect2) 50)
;; (= (distTo0 dot1) 30)
;; (= (distTo0 dot2) 80)
;; Template:
;; ... (cond
;; [(circle? s) ...]
;; [(rect? s) ...]
;; [(dot? s) ...]
;; But, looking at the structures for each kind of shape we get:
;; ... (cond
;; [(circle? s) ...(circle-center s) ... (circle-radius s) ...]
;; [(rect? s) ...(rect-nw s) ... (rect-width s) ... (rect-height s) ...]
;; [(dot? s) ...(posn-x s) ... (posn-y s) ...]
;; Program:
(define (distTo0 s)
(cond
[(circle? s) (dist (circle-center s) (make-posn 0 0))]
[(rect? s) (dist (rect-nw s) (make-posn 0 0))]
[(posn? s) (dist s (make-posn 0 0))] ))
;; Tests:
(= (distTo0 cir1) 30)
(= (distTo0 cir2) 50)
(= (distTo0 rect1) 30)
(= (distTo0 rect2) 50)
(= (distTo0 dot1) 30)
(= (distTo0 dot2) 50)
Example: counting the number of items in a list
;; A List-of-Auto is either ;; - the empty list, or ;; - (cons a loa) where a is an Auto and loa is a List-of-Auto ;; count the number of autos in the loa list ;; List-of-Auto -> Number ;; (define (count loa) ... ;; Examples: ;; (= (count mt-loa) 0) ;; (= (count loa1) 1) ;; (= (count loa2) 2) ;; (= (count loa3) 3) ;; Template: ;; (cond ;; [(empty? loa) ...] ;; [else ... (first loa) ... ;; ... (count (rest loa)) ...] ;; Program: (define (count loa) (cond [(empty? loa) 0] [else (+ 1 (count (rest loa)))])) ;; Tests: (= (count mt-loa) 0) (= (count loa1) 1) (= (count loa2) 2) (= (count loa3) 3)