2
votes

I'm not sure what's going wrong here, I believe my syntax is correct but I've no idea what the error is trying to say.

(define (even-nm-inst? n)
(cond ((= n 2) #t)
    ((= n 4) #t)
    ((= n 6) #t)
    ((= n 8) #t)
    ((= n 10) #t)
    ((> n 2)
     (modulo n 2)
     (cond ((= n 0) #t)
     (cond ((= n 1) #f)
    ((< n 2)            
     (modulo n 2)       
     (cond ((= n 0) #t)
           ((= n 1) #f)
           )))))))

I'm trying to make a function that determines if a number is even or not.

The specific error returned is: . ../../Applications/Racket v6.6/share/pkgs/r5rs-lib/r5rs/main.rkt:493:22: with-syntax: binding match failed in: (_ . rest)

Any idea what's going on? I know it's this specific part of code because I have headers on the different sections, and the error appears under the header for this chunk.

Thanks in advance, I'm brand new to scheme.

3

3 Answers

1
votes

No, that's not correct – your conds are all over the place, and apparently so much so that Racket is getting confused.

It's not clear at all why you wrote all that code;

(define (even-nm-inst? n)
    (= (modulo n 2) 0))

would do, as would using even?.

1
votes

Well, this is an inexcusably bad syntax error...

but on the other hand, it looks like you're using the extremely-legacy R5RS language. If that's because you're in a class and your instructor requires it, then you're stuck with it. If not, though, you should definitely switch ASAP to the main Racket language, or the Beginning Student language; neither of them should give you that terrible message.

Specifically, in the Beginning Student language, you'll get this message: "cond: expected a clause with a question and an answer, but found a clause with 3 parts" ... and it highlights the three parts of the clause that are the problem.

1
votes

Since you are using DrRacket why don't you press CTRL+i and it will format the code according to what you have written and not what you would like to have written. THe result is:

(define (even-nm-inst? n)
  (cond ((= n 2) #t)
        ((= n 4) #t)
        ((= n 6) #t)
        ((= n 8) #t)
        ((= n 10) #t)
        ((> n 2)
         (modulo n 2) ; doesn't do anything since result not used
         (cond ((= n 0) #t)
               (cond ((= n 1) #f) ; cond doesn't exist as a variable
                     ((< n 2)            
                      (modulo n 2)       
                      (cond ((= n 0) #t) ; cond doesn't exist as a variable
                            ((= n 1) #f)
                            )))))))

Now. If using even? is not an option I would have written this:

(define (my-even? n)
  (cond ((negative? n) (my-even? (- n)))
        ((zero? n) #t)
        ((= n 1) #f)
        (else (my-even? (- n 2)))))

(my-even? -5) ; ==> #f
(my-even? 10) ; ==> #t

You use modulo and it can be used to produce a result without any conditionals:

(define (my-even? n)
  (= 0 (modulo n 2)))