Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclusive OR in Scheme

Tags:

logic

lisp

scheme

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.

like image 576
Jeffrey Aylesworth Avatar asked Dec 18 '09 21:12

Jeffrey Aylesworth


People also ask

What is an expression in Scheme?

A Scheme expression is a construct that returns a value, such as a variable reference, literal, procedure call, or conditional. Expression types are categorized as primitive or derived. Primitive expression types include variables and procedure calls.

How do you define a constant in a Scheme?

Just define the value at the toplevel like a regular variable and then don't change it. To help you remember, you can adopt a convention for naming these kinds of constants - I've seen books where toplevel variables are defined with *stars* around their name.

What is begin in Scheme?

Scheme begin expressions aren't just code blocks, though, because they are expressions that return a value. A begin returns the value of the last expression in the sequence. For example, the begin expression above returns the value returned by the call to bar . The bodies of procedures work like begin s as well.

How does let work in Scheme?

In Scheme, you can use local variables pretty much the way you do in most languages. When you enter a let expression, the let variables will be bound and initialized with values. When you exit the let expression, those bindings will disappear.


2 Answers

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?

like image 192
Andrew Avatar answered Sep 28 '22 19:09

Andrew


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.

like image 45
Dale Hagglund Avatar answered Sep 28 '22 20:09

Dale Hagglund