我似乎在使用以下代码时遇到了一些问题:
(define-struct speise (vegan name))
(define (vegan? speise1 speise2 speise3)
(cond
[(and (equal? speise1 true)
(and (equal? speise2 true)
(equal? speise3 true))) true]
[else false]))
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise false "Mettkipferl") (make-speise true "Chilli-Spekulatius")) #false)
(check-expect (vegan? (make-speise false "Kuerbis-Fleisch-Suppe") (make-speise false "Mettkipferl") (make-speise false "Chilli-Fleisch-Spekulatius")) #false)
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise true "Salatkipferl") (make-speise true "Chilli-Spekulatius")) #true)
前两个测试没问题,但第三个测试导致异常,因为实际值与预期值不同。 我只想检查所有3项 - 如果所有3项都是真的,那么我只想让程序打印为true。如果这3个中的一个是假的,那么它应该打印为false。 很抱歉,这些变量都是德语。
答案 0 :(得分:1)
那将是
(define-struct speise (vegan name))
(define (vegan? speise1 speise2 speise3)
(and (speise-vegan speise1)
(speise-vegan speise2)
(speise-vegan speise3)))
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise false "Mettkipferl") (make-speise true "Chilli-Spekulatius")) #false)
(check-expect (vegan? (make-speise false "Kuerbis-Fleisch-Suppe") (make-speise false "Mettkipferl") (make-speise false "Chilli-Fleisch-Spekulatius")) #false)
(check-expect (vegan? (make-speise true "Kuerbis-Marzipan-Suppe") (make-speise true "Salatkipferl") (make-speise true "Chilli-Spekulatius")) #true)
(check-expect (vegan? (make-speise true "Something") (make-speise false "Somethingelse") (make-speise true "idontknow")) #false)
执行
Welcome to DrRacket, version 6.10.1 [3m].
Language: Beginning Student; memory limit: 128 MB.
All 4 tests passed!
>