2010-04-30 7 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) 

Il produit l'exception suivante premier: argument attendu de type liste non-vide; donné 'Fantasy, je ne comprends pas quel est le problème.schéma struct question

Quelqu'un peut-il me donner une explication?

Merci beaucoup!

Répondre

1

Dans l'appel récursif à count-books-for-genre vous mélangez l'ordre des arguments.

I.e. vous passez en (rest lob) comme premier argument (genre) et genre comme second (lob). Donc, dans le premier appel récursif lob est en réalité 'Fantasy au lieu de (rest some-books) et donc en essayant d'utiliser les opérations de liste sur elle conduit à l'échec.