karthik A
karthik A

Reputation: 655

Why do I get unbound variable error in Scheme?

(define (even-odd x)
(if ( ==(% x 2 ) 0) (1) (0)))

when i call( even-odd 5 ) i get this message

;Unbound variable: %
;To continue, call RESTART with an option number:
; (RESTART 11) => Specify a value to use instead of %.
; (RESTART 10) => Define % to a given value.
; (RESTART 9) => Return to read-eval-print level 9.
; (RESTART 8) => Return to read-eval-print level 8.
; (RESTART 7) => Return to read-eval-print level 7.
; (RESTART 6) => Return to read-eval-print level 6.
; (RESTART 5) => Return to read-eval-print level 5.
; (RESTART 4) => Return to read-eval-print level 4.
; (RESTART 3) => Return to read-eval-print level 3.
; (RESTART 2) => Return to read-eval-print level 2.
; (RESTART 1) => Return to read-eval-print level 1.

;Start debugger? (y or n):

Am i doing something wrong ?Also let me know how to select text in edwin.

Upvotes: 3

Views: 8847

Answers (3)

Óscar López
Óscar López

Reputation: 235984

This is a more idiomatic way of writing the even-odd procedure:

(define (even-odd x)
    (if (zero? (modulo x 2))
        #t
        #f))

A bit shorter:

(define (even-odd x)
    (zero? (modulo x 2)))

Or even better, use the built-in procedure even?:

(even? x)

Upvotes: 3

dagoof
dagoof

Reputation: 1139

You have a few problems here; namely that you're mistaken in the names of the procedures you're trying to call.

  • % should be modulo
  • == should be eqv?
  • As zvrba noted, remove parenthesis surrounding 0 and 1

or, if you're simply trying to tell whether a number is even or odd, a simpler and cleaner way to do this is to simply use the builtin even?.

(even? 5)
> #f

or if you actually want 0 and 1 as a result, a cleaner expression could be

(if (even? x) 1 0)

Upvotes: 4

zvrba
zvrba

Reputation: 24546

You must omit parentheses around the return values (1 and 0). What you wrote tries to invoke procedures named 1 and 0. Also, the modulus operator isn't %, but is named otherwise [check the manual; I forgot it -- probably mod or rem].

Upvotes: 3

Related Questions