Scheme 方案结构问题
它首先产生以下异常:类型为非空列表的预期参数;考虑到“幻想”,我不明白问题出在哪里 谁能给我解释一下吗Scheme 方案结构问题,scheme,Scheme,它首先产生以下异常:类型为非空列表的预期参数;考虑到“幻想”,我不明白问题出在哪里 谁能给我解释一下吗 多谢各位 在递归调用中,为流派计算书籍数量时,您混淆了参数顺序 也就是说,您将(rest lob)作为第一个参数(类型)传递,将类型作为第二个参数(lob)。因此,在第一个递归调用中,lob实际上是“幻想”,而不是(其他一些书籍),因此尝试对其使用列表操作会导致失败 ;; definition of the structure "book" ;; author: string - the au
多谢各位 在递归调用中,为流派计算书籍数量时,您混淆了参数顺序 也就是说,您将
(rest lob)
作为第一个参数(类型)传递,将类型作为第二个参数(lob)。因此,在第一个递归调用中,lob实际上是“幻想”,而不是(其他一些书籍)
,因此尝试对其使用列表操作会导致失败
;; 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)