Here is one solution.
;; inventory-total-value : LOB -> Number ;; GIVEN: a LOB ;; RETURNS: the value of all the copies on hand of all the books in the ;; given LOB ;; (inventory-total-value lob1) = 390 ;; EXAMPLES/TESTS: (begin-for-test (check-equal? (inventory-total-value empty) 0 "value of the empty inventory should have been 0") (check-equal? (inventory-total-value lob1) 390 "simple test")) ;; STRATEGY: Structural Decomposition on lob : LOB (define (inventory-total-value lob) (cond [(empty? lob) 0] [else (+ (book-inventory-value (first lob)) (inventory-total-value (rest lob)))]))
;; books-out-of-stock : LOB -> LOB ;; returns a list of the books that are out of stock in the given LOB ;; Example: ;; (books-out-of-stock lob1) = ;; (list ;; (make-book "Shakespeare" "Hamlet" 0 2) ;; (make-book "Shakespeare" "Macbeth" 0 10)) ;; Strategy: structural decomposition on lob : LOB (define (books-out-of-stock lob) (cond [(empty? lob) empty] [else (if (book-out-of-stock? (first lob)) (cons (first lob) (books-out-of-stock (rest lob))) (books-out-of-stock (rest lob)))])) (check-equal? (books-out-of-stock lob1) (list (make-book "Shakespeare" "Hamlet" 0 2) (make-book "Shakespeare" "Macbeth" 0 10))) ;; book-out-of-stock? : Book -> Boolean ;; returns true iff the given book is out of stock ;; EXAMPLE: ;; (book-out-of-stock? (make-book "Felleisen" "HtDP/1" 20 7)) = false ;; (book-out-of-stock? (make-book "Felleisen" "HtDP/1" 0 7)) = true ;; STRATEGY: stuctural decomposition on b : Book (define (book-out-of-stock? b) (<= (book-on-hand b) 0)) (begin-for-test (check-false (book-out-of-stock? (make-book "Felleisen" "HtDP/1" 20 7))) (check-true (book-out-of-stock? (make-book "Felleisen" "HtDP/1" 0 7)))) ;; (check-equal? ... true) would have been fine, too. Look at the ;; Help Desk page for check-equal? to see some other useful checks.
Note that the design of the help function book-out-of-stock?, with its deliverables, is required for a perfect solution. Writing
(if (<= (book-on-hand (first lob)) 0) ... ...)matches the template, but it is undesirable because it violates the principle of one-function-one-task. It violates this principle because this version of the function requires books-out-of-stock to know both about the representation of an inventory AND the how being out of stock is represented for a book.
Last modified: Tue Aug 12 10:53:24 Eastern Daylight Time 2014