;; 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 lect02bc-structs) (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")))))
; 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.]
; 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" 1.3 true)
(make-dillo "Pickles" 1.5 false)
; 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" 1.3 true))
(make-dillo "Phyllis" 2.3 false))
(check-expect (hit-with-truck (make-dillo "Pickles" 1.5 false))
(make-dillo "Pickles" 2.5 false))
; Data definition:
; An Animal is
; - a boa, or
; - a dillo.
; 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) 1)]
))
; List of Animals, for a zoo:
; Data definition:
; A list-of-animal is:
; - empty
; - (cons animal list-of-animal)
; Examples of the data: