;; structure representing homework points
;; nr: number - the number of the homework
;; points: number - the number of points reached
(define-struct homework (nr points))
;; parse-homework: (list of number pairs) -> (list of homework)
;; The procedure takes a list of number pairs and produces a list of homework structures
;; Example: (parse-homework (list (list 1 6) (list 2 7) (list 3 0))) should produce (list (make-homework 1 6) (make-homework 2 7) (make-homework 3 0))
(define (parse-homework homework-entries)
(if (and (= (length (first homework-entries) 2))(= (length (parse-homework (rest homework-entries)) 2)))
(make-homework (first homework-entries) (parse-homework (rest homework-entries)))
(error 'Non-valid-input "entered list is not of length two"))
)
(parse-homework (list (list 1 6) (list 2 7) (list 3 0))) 这段代码产生错误长度:期望1参数,给定2:(列表1 6) 2
我真的很感激你能给我的每一个解释让我参与这个计划.
非常感谢
发布于 2010-05-10 15:32:40
你的父母错了(见下文)
(define (parse-homework homework-entries)
(if (and (= (length (first homework-entries) 2)) ;; <---- Parens wrong here
(= (length (parse-homework (rest homework-entries)) 2))) ;; <---- ... and here
(make-homework (first homework-entries) (parse-homework (rest homework-entries)))
(error 'Non-valid-input "entered list is not of length two"))
) 您需要用一个参数调用length函数,用两个参数调用=函数:
(= (length (first homework-entries)) 2)对于另一条标记行也是如此。
编辑在解析家庭作业列表时,请考虑:
homework-entries的所有元素?也就是说,您什么时候必须停止递归?(null?)错误说明了一切:输入列表是null?,这是按照您的示例将parse-homework应用于项目列表的预期结果?你实际上并没有产生一个有意义的结果。试着把问题分解成更小的部分:
(define (parse-homework-item item)
;; Parse a single homework item, returning an instance of the
;; Homework type for it.
...)
(define (parse-homework list-of-items)
;; Loop over the elements of list-of-items, processing each in turn
;; using parse-homework-item. Collect each result object into a list
;; which is returned as final result.
...)https://stackoverflow.com/questions/2803856
复制相似问题