tags:

views:

351

answers:

4

What is the exclusive or functions in scheme? I've tried xor and ^, but both give me an unbound local variable error.

Googling found nothing.

+6  A: 

If you mean bitwise xor of two integers, then each Scheme has it's own name (if any) since it's not in any standard. For example, PLT has these bitwise functions, including bitwise-xor.

(Uh, if you talk about booleans, then yes, not & or are it...)

Eli Barzilay
+5  A: 

I suggest you use (not (equal? foo bar)) if not equals works. Please note that there may be faster comparators for your situiation such as eq?

Andrew
So Scheme doesn't provide a not equal to function? You have to roll your own with not and (=|eq?|equal?)?
Jeffrey Aylesworth
Scheme is a fairly minimalist language. From R<sup>5</sup>RS: "Programming languages should be designed not by piling feature on top of feature, but by removing the weaknesses and restrictions that make additional features appear necessary." So, there are a lot of things provided by other languages that Scheme merely provides building blocks for. Any given Scheme implementation may provide a more complete library, and the SRFIs http://srfi.schemers.org/ help standardize a few of the extensions to the core language.
Brian Campbell
Okay, thank you
Jeffrey Aylesworth
+5  A: 

As far as I can tell from the R6RS (the latest definition of scheme), there is no pre-defined exclusive-or operation. However, xor is equivalent to not equals for boolean values so it's really quite easy to define on your own if there isn't a builtin function for it.

Assuming the arguments are restricted to the scheme booleans values #f and #t,

(define (xor a b)
  (not (boolean=? a b)))

will do the job.

Dale Hagglund
+2  A: 

Kind of a different style of answer:

(define xor
  (lambda (a b)
    (cond
      (a (not b))
      (else b))))   
danielrsmith