2010-04-30 48 views
0
;; definition of the structure "book" 
;; author: string - the author of the book 
;; title: string - the title of the book 
;; genre: symbol - the genre 
(define-struct book (author title genre)) 

(define lotr1 (make-book "John R. R. Tolkien" 
         "The Fellowship of the Ring" 
         'Fantasy)) 
(define glory (make-book "David Brin" 
         "Glory Season" 
         'ScienceFiction)) 
(define firstFamily (make-book "David Baldacci" 
           "First Family" 
           'Thriller)) 
(define some-books (list lotr1 glory firstFamily)) 

;; count-books-for-genre: symbol (list of books) -> number 
;; the procedure takes a symbol and a list of books and produces the number   
;; of books from the given symbol and genre 
;; example: (count-books-for-genre 'Fantasy some-books) should produce 1 
(define (count-books-for-genre genre lob) 

(if (empty? lob) 0 
(if (symbol=? (book-genre (first lob)) genre) 
     (+ 1 (count-books-for-genre (rest lob) genre)) 
     (count-books-for-genre (rest lob) genre) 
    )  
)  
)    

(count-books-for-genre 'Fantasy some-books) 

它產生以下異常第一:非空類型的預期參數;鑑於'幻想,我不明白什麼是問題。方案結構問題

有人可以給我一些解釋嗎?

非常感謝!

回答

1

在count-books-for-genre的遞歸調用中,混合了參數順序。

I.e.你傳入(rest lob)作爲第一個參數(流派)和流派作爲第二個(lob)。所以在第一次遞歸調用中,lob實際上是'Fantasy'而不是(rest some-books),因此試圖對其使用列表操作會導致失敗。