解析列表并生成该列表的结构
;; 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)))
这段代码产生错误 length: Expects 1 argument, 鉴于 2: (list 1 6) 2
我真的很感激你能给我的每一个解释,让我参与这个方案......
谢谢非常
;; 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)))
This code produces the error length: expects 1 argument, given 2: (list 1 6) 2
I really appreciate every explanation that you can give me to get in in this scheme-stuff...
Thank you very much
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的括号是错误的(见下文)
您需要使用一个参数调用
length
函数,使用两个参数调用=
函数:对于另一条标记行也是如此。
编辑 当您解析家庭作业列表时,请考虑:
homework-entries
的所有元素?即,什么时候必须停止递归? (null?
) 错误说明了一切:输入列表已用尽。parse-homework
应用于项目列表的预期结果是什么?您实际上并没有产生有意义的结果。尝试将问题分解为更小的部分:
Your parens are wrong (see below)
You need to call the
length
function with one, the=
function with two arguments:Likewise for the other marked line.
Edit When you parse the list of homework assignments, consider:
homework-entries
? I.e., when do you have to stop recursing? (null?
) The errors says it all: the input list was exhausted.parse-homework
to a list of items as per your example? You are not actually generating a meaningful result.Try to decompose the problem into smaller parts: