0

試験対策でこの問題集をやっています。それは本にあります。基本的に、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))))
4

1 に答える 1