麻烦一个程序

时间:2013-11-06 21:11:02

标签: 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.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))))

1 个答案:

答案 0 :(得分:0)

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

单击“步骤”按钮,等待所有步骤都计算完毕(您将看到分母在步数中停止变化)。然后,从下拉菜单中选择“跳到结尾”。你会看到你的测试用例要求(第一个'ol)。要了解这是如何产生的,请退一步;我可以看到你正在调用is-attribute?在'ol,但我没有再进一步了解为什么会这样。