解析列表并生成该列表的结构

发布于 2024-08-31 19:27:25 字数 1021 浏览 3 评论 0原文

;; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自←快乐 2024-09-07 19:27:25

您的括号是错误的(见下文)

(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?) 错误说明了一切:输入列表已用尽。
  • 根据您的示例,将 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.
    ...)

Your parens are wrong (see below)

(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"))

)      

You need to call the length function with one, the = function with two arguments:

(= (length (first homework-entries)) 2)

Likewise for the other marked line.

Edit When you parse the list of homework assignments, consider:

  • When have you parsed all elements of homework-entries? I.e., when do you have to stop recursing? (null?) The errors says it all: the input list was exhausted.
  • What is the intended result of applying 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:

(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.
    ...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文