List 程序故障

List 程序故障,list,scheme,racket,List,Scheme,Racket,我做这道题是为了准备考试。它在书里。基本上,它告诉我们设计一个程序来统计XEnum.v2实例中出现的所有“hello”。 除了这个问题,一切都很好。我这张支票有问题 (check-expect (count-in-xitem xitem3) 3) 它说它期望一个非空列表,但给出了“ol”。它绝对应该给我3分。为什么它总是告诉我它需要一个非空的列表?我目瞪口呆,不知道为什么 ;; An XEnum.v2 is one of: ;; – (cons 'ol [List-of XItem

我做这道题是为了准备考试。它在书里。基本上,它告诉我们设计一个程序来统计XEnum.v2实例中出现的所有“hello”。 除了这个问题,一切都很好。我这张支票有问题

(check-expect (count-in-xitem xitem3) 3)
它说它期望一个非空列表,但给出了“ol”。它绝对应该给我3分。为什么它总是告诉我它需要一个非空的列表?我目瞪口呆,不知道为什么

  ;; An XEnum.v2 is one of:
    ;; – (cons 'ol [List-of XItem.v2])
    ;; – (cons 'ol (cons [List-of Attribute] [List-of XItem.v2]))

;; An XItem.v2 is one of:
;; – (cons 'li (cons XWord empty))
;; – (cons 'li (cons [List-of Attribute] (cons XWord empty)))
;; – (cons 'li (cons XEnum.v2 empty))
;; – (cons 'li (cons [List-of Attribute] (cons XEnum.v2 empty)))

;; A XWord is '(word ((text String)))

;; An Attribute is
;; - (cons Symbol (cons String empty))

(define xword1 '(word ((text "hello"))))
(define xword2 '(word ((text "Hello"))))
(define attr1 (cons 'Symbol (cons "hello" empty)))
(define attr2 (cons 'Symbol (cons "elo" empty)))

(define xitem1 (cons 'li (cons xword1 empty)))
(define xitem2 (cons 'li (cons (list attr1 attr2) (cons xword1 empty))))
(define xe1 (cons 'ol (list xitem1 xitem2))) ;; 3
(define xe2 (cons 'ol (cons (list attr1 attr2) (list xitem2 xitem1))))
(define xitem3 (cons 'li (cons xe1 empty))) ;; 1
(define xitem4 (cons 'li (cons (list attr1 attr2) (cons xe1 empty))))



;; X-Item.v2 -> Number
;; returns number of "hello" occurences in an X-Item.v2
(define (count-in-xitem xi)
  (cond
    [(is-xword? (second xi)) (count-in-xword (second xi))]
    [(is-xenum? xi) (+ (count-in-xitem (second xi))
                       (count-in-xitem (second (rest xi))))]
    [(is-attribute? (first (second xi))) (+ (count-in-loa (second xi))
                                            (count-in-xword (first (rest (rest xi)))))]

    [else (+ (count-in-loa (second xi))
             (occurrences (second (rest xi))))]))

;; tests for count-in-xitem function
;(check-expect (count-in-xitem xitem1) 1)
;(check-expect (count-in-xitem xitem2) 2)
;(check-expect (count-in-xitem xe1) 3)
(check-expect (count-in-xitem xitem3) 3)




;; XWord -> Natural
;; returns 1 if string is "hello"
(define (count-in-xword x) 
  (if (string=? (second (first (first (rest x))))
                "hello")
      1
      0))

;; tests for count-in-xword function
(check-expect (count-in-xword xword1) 1)
(check-expect (count-in-xword xword2) 0)


;; [List-of Attribute] -> Natural
;; returns 1 if occurrences of "hello" in the list of attributes                              
(define (count-in-loa loa) 
  (foldr (λ(s b) (if (string=? (second s) "hello") (+ 1 b) b)) 0 loa))

;; tests for count-in-loa function
(check-expect (count-in-loa (list attr2)) 0)
(check-expect (count-in-loa (list attr1
                                  (cons 'b (cons "hello" empty)))) 2)



;; XEnum.v2 -> Number
;; counts all occurrences of "hello" in an instance of XEnum.v2
(define (occurrences xe)
  (if (eqv? (rest (rest xe)) empty)
      (xenum2 empty (rest xe))
      (xenum2 (second xe) (rest (rest xe)))))

;; [List-of Attribute] [List-of XItem.v2] -> Number
;; returns number of "hello" occurences
(define (xenum2 atr item)
  (+ (count-in-loa atr)
     (count-in-xitem item)))


;; tests for xenum2 function
;(check-expect (xenum2 (list attr1 attr2) (list xitem1 xitem2)) 0)


;; [List-of Any] -> Boolean
;; checks if the list is an XEnum.v2
(define (is-xenum? xe)
  (cond
    [(empty? xe) false]
    [(symbol? (first xe))
     (symbol=? (first xe) 'ol)]))


;; tests for is-attribute? function
(check-expect (is-xenum? xe1) true)
(check-expect (is-xenum? xe2) true)
(check-expect (is-xenum? (cons 'al (list xitem1 xitem2))) false)
(check-expect (is-xenum? empty) false)


;; ATTRIBUTE
(define (is-attribute? xe)
  (and (symbol? (first xe))
       (string? (second xe))
       (not (symbol=? (first xe) 'ol))))

;; tests for is-attribute? function
(check-expect (is-attribute? attr1) true)
(check-expect (is-attribute? attr2) true)
(check-expect (is-attribute? (cons 1 (cons "hi" empty))) false)


;; XWORD
(define (is-xword? xe)
  (and (symbol? (first xe))
       (symbol=? 'word (first xe))
       (symbol=? 'text (first (first (second '(word ((text String)))))))
       (symbol=? 'String (second (first (second '(word ((text String)))))))))

;; tests for is-xword? function
(check-expect (is-xword? xword1) true)
(check-expect (is-xword? xword2) true)
(check-expect (is-xword? '(world ((text "hello")))) false)
我认为问题就在这里

;; ATTRIBUTE
(define (is-attribute? xe)
  (and (symbol? (first xe))
       (string? (second xe))
       (not (symbol=? (first xe) 'ol))))

看起来您可以使用步进器跟踪此问题

单击“步骤”按钮,等待所有步骤计算完毕(您将看到分母在步骤计数中停止变化)。然后,从下拉菜单中选择“跳到终点”。您将看到您的测试用例要求(第一个'ol')。要了解这是如何产生的,请后退一步;我看得出你打电话的是属性?但我没有退后一步去看看为什么会这样