Problem 2. Mrs. Stricktor, a snake collector, keeps track of her collection with ``snake records'' such as these:
(define-struct boa (name dob length weight)) ;; A Snake is a structure: ;; (make-boa String Date Number Number). (define-struct date (year month day)) ;; A Date is a structure: ;; (make-date Number Number Number).
Because she has been bitten recently, she is going to out-source the next software job. Your company got the contract. And so your manager has just handed you this task:
Design a function that computes the age of a boa in years from a snake record and the current date.
Solution:
;; Grader: Felix ;; snake-age : Snake Date -> Number [1pt] ;; determine the age of the snake in years [1pt] ;; example: given: [1pt] ;; (make-boa "bella" (make-date 1988 11 28) 10 20) ;; (make-date 2005 10 18) ;; wanted: ;; 17 (define (snake-age b current-date) (date-year-delta current-date (boa-dob b))) ;; [1pt for structure selector] ;; [+4pts, if date-year-delta is properly in-lined] ;; date-year-delta : Date Date -> Number [1pt] ;; subtract number of years of second from first [1pt] ;; example: given: [0pt] ;; (make-date 1988 11 28) ;; (make-date 2005 10 18) ;; wanted: ;; 17 (define (date-year-delta d1 d2) (- (date-year d1) (date-year d2))) ;; [1pt for structure selector, 1pt for correctness] ;; Tests: [2pts for proper # of correct tests, ;; 3pts if no examples] (equal? (date-year-delta (make-date 1988 11 28) (make-date 2005 10 18)) 17) (equal? (snake-age (make-boa "bella" (make-date 1988 11 28) 10 20) (make-date 2005 10 18)) 17)