Spaceputin
Spaceputin

Reputation: 3

How do i use 'equal?' properly in this case?

i seem to have some trouble with the following code:

   (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)

The first 2 tests are fine, but the third one results in an exception, because the actual value differs from the expected one. I just want to check all 3 items - if all 3 are true, then i just want the programm to print true. If one of these 3 is false, it's supposed to print false. Sorry that the variables are in german though.

Upvotes: 0

Views: 47

Answers (1)

uselpa
uselpa

Reputation: 18917

That would be

(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)

executing:

Welcome to DrRacket, version 6.10.1 [3m].
Language: Beginning Student; memory limit: 128 MB.
All 4 tests passed!
> 

Upvotes: 1

Related Questions