繁体   English   中英

方案结构问题

[英]scheme struct question

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

它首先产生以下异常:非空列表类型的期望参数; 给定“幻想 ,我不明白是什么问题。

有人可以给我一些解释吗?

非常感谢你 !

在对流传计数书的递归调用中,您混淆了参数顺序。

也就是说,您传入(rest lob)作为第一个参数(体裁),并将体裁作为第二个参数(lob)。 因此,在第一个递归调用lob中,实际上是'Fantasy而不是(rest some-books) ,因此尝试对其使用列表操作会导致失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM