开发者

parsing a list and producing a structure of that

开发者 https://www.devze.com 2022-12-29 23:49 出处:网络
;; structure representing homework points ;; nr: number - the number of the homework ;; points: number - the number of points reached
;; 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


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.
    ...)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号