tags:

views:

96

answers:

1
;; 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)

It produce following exception first: expected argument of type non-empty list; given 'Fantasy, I don't understand whats the problem.

Can somebody give me some explanation ?

Thank you very much !

+1  A: 

In the recursive call to count-books-for-genre you're mixing up the argument order.

I.e. you pass in (rest lob) as the first argument (genre) and genre as the second (lob). So in the first recursive call lob is actually 'Fantasy instead of (rest some-books) and thus trying to use list operations on it leads to failure.

sepp2k