方案结构问题

时间:2010-04-30 23:05:02

标签: scheme

;; 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 个答案:

答案 0 :(得分:1)

在递归调用count-books-for-genre时,你混淆了参数顺序。

即。你传递(rest lob)作为第一个参数(流派)和流派作为第二个参数(lob)。所以在第一个递归调用中,lob实际上是'Fantasy而不是(rest some-books),因此尝试在其上使用list操作会导致失败。