;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-advanced-reader.ss" "lang")((modname lect03a) (read-case-sensitive #t) (teachpacks ((lib "world.ss" "teachpack" "htdp"))) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ((lib "world.ss" "teachpack" "htdp")))))
#| The design recipe:
1. Data Def'n
2. Examples of Data
3. Template for functions handling the data
4a Write function header
b Write function comments
c Write test cases
d Write function body
5. run tests
|#
; Data definition:
; A boa is:
; (make-boa name length)
; where length is in meters.
(define-struct boa (name length))
; make-boa: (-> string number boa)
; Examples of the data:
(make-boa "Minime" 0.2)
(make-boa "Nidhogg" 23)
(define bo (make-boa "Mr. Bojangles" 3.3))
; Creates a struct 'boa', along with:
; constructor: make-boa
; accessors: boa-name, boa-length
; predicate: boa?
; mutators: set-boa-name!, set-boa-length! [DISALLOWED for this class.]
;;; A template, for *any* function involving boas:
#;(define (boa-fun a-boa)
··· (boa-name a-boa) ··· (boa-length a-boa) ··· )
; can-fit?: (-> boa number boolean)
; Can a-boa fit in a carrier whose length is carrier-len?
;
(define (can-fit? a-boa carrier-len)
(<= (boa-length a-boa) carrier-len))
(check-expect (can-fit? (make-boa "Slinky" 3) 2)
false)
(check-expect (can-fit? (make-boa "Slinky" 3) 4)
true)
(check-expect (can-fit? (make-boa "Slinky" 3) 3)
true)
; Data definition:
; A dillo is:
; (make-dillo [string] [number] [boolean])
;
(define-struct dillo (name length alive?))
; Examples:
(make-dillo "Phyllis" 0.3 true)
(make-dillo "Pickles" 0.6 false)
;;; Template for any dillo-handling function:
#;(define (fun-for-dillo d)
···(dillo-name d) ··· (dillo-length d) ··· (dillo-alive? d) )
; hit-with-truck: (-> dillo dillo)
;
(define (hit-with-truck a-dillo)
(make-dillo (dillo-name a-dillo) (+ (dillo-length a-dillo) 1) false) )
(check-expect (hit-with-truck (make-dillo "Phyllis" 0.3 true))
(make-dillo "Phyllis" 1.3 false))
(check-expect (hit-with-truck (make-dillo "Pickles" 0.6 false))
(make-dillo "Pickles" 1.6 false))
; Data definition:
; An Animal is
; - a boa, or
; - a dillo.
;;; Template for *any* animal-handling function:
#;(define (animal-fun a)
(cond [(boa? a) (boa-length a) ··· (boa-name a)]
[(dillo? a) (dillo-length a) ··· (dillo-alive? a)··· (dillo-name a)]
))
;; Define:
; grow-animal : (-> animal num animal)
; Grow an animal by the given percentage.
;
(define (grow-animal a pct)
(cond [(boa? a) (make-boa (boa-name a)
(* (+ 1 (/ pct 100)) (boa-length a)))]
[(dillo? a) (make-dillo (dillo-name a)
(* (+ 1 (/ pct 100)) (dillo-length a))
(dillo-alive? a))]
))
(check-expect (grow-animal (make-boa "Minime" 0.2) 50)
(make-boa "Minime" 0.3))
(check-expect (grow-animal (make-dillo "Phyllis" 0.3 true) 100)
(make-dillo "Phyllis" 0.6 true))
(check-expect (grow-animal (make-dillo "Pickles" 1.6 false) 50)
(make-dillo "Pickles" 2.4 false))
; animal-length : (-> animal num)
; Return the length of any animal.
(define (animal-length a)
(cond [(boa? a) (boa-length a)]
[(dillo? a) (dillo-length a)]
))
(check-expect (animal-length (make-boa "Minime" 0.2)) 0.2)
(check-expect (animal-length (make-dillo "Phyllis" 0.3 true)) 0.3)
(check-expect (animal-length (make-dillo "Pickles" 1.6 false)) 1.6)
; too-long? : (-> animal boolean)
; Is an animal unsually long?
; (More than 1m for a dillo, and more than 3m for a boa.)
;
(define (too-long? a)
(cond [(boa? a) (> (boa-length a) 3)]
[(dillo? a) (> (dillo-length a) 0.5)]
))
(check-expect (too-long? (make-boa "Minime" 0.2)) false)
(check-expect (too-long? (make-dillo "Phyllis" 0.3 true)) false)
(check-expect (too-long? (make-dillo "Pickles" 1.6 false)) true)
(check-expect (too-long? bo) true)
; List of Animals, for a zoo:
; Data definition:
; A list-of-animal is:
; - empty
; - (cons [animal] [list-of-animal])
; Examples of the data:
empty
(define loa1 (cons (make-boa "Minime" 0.3) empty))
(define loa2 (cons (make-dillo "Pickles" 1.6 false) loa1))
(define loa3
(cons (make-dillo "Phyllis" 0.3 true)
(cons (make-dillo "Pickles" 1.6 false)
(cons (make-boa "Minime" 0.3)
empty))))
;;; TEmplate for any list-of-Animal-handling function:
#;(define (fun-for-loa aloa)
(cond [(empty? aloa) ...]
[(cons? aloa) (first aloa) ... (rest aloa)]))
; total-len : (-> (list-of animal) num)
(define (total-len aloa)
(cond [(empty? aloa) 0]
[(cons? aloa) (+ (animal-length (first aloa))
(total-len (rest aloa)))]))
(check-expect (total-len empty) 0)
(check-expect (total-len loa1) 0.3)
(check-expect (total-len loa2) 1.9)
(check-expect (total-len loa3) 2.2)
;; Write:
; num-too-long : (-> (list-of animal) natnum)
; Count how many animals in the list are too long.
;; Write:
; collect-long-animals: (-> (list-of animal) (list-of animal))
; grow-all: (-> (list-of animal) (list-of animal))
;; Just so we can compile even if we use ··· in stub functions above:
(define ··· "a stub value")
;; (Note that we can't use '...'; this is reserved in Scheme to
;; indicate repetition of a macro pattern.)