Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test equality of quoted symbols in Scheme?

In this example,

> (= 1 1)
#t
> (= 'a 'a)
*** ERROR IN (console)@2.1 -- (Argument 1) NUMBER expected
(= 'a 'a)
1> 

How can I test equality of quoted symbols?

like image 606
eonil Avatar asked Jun 13 '11 09:06

eonil


1 Answers

You use any of eq?, eqv?, or equal?.

All three can be used with any objects without error (unlike =, which can only be used with numbers). However, the result will differ depending on which types you pass in. But if you know you're comparing symbols, all of them will have the same result.

If you have done any Java programming, eqv? is like ==, and equal? is like .equals(). In simple terms, eqv? does an identity comparison, and equal? does a value comparison.

(And eq? does a straight-up pointer comparison. For some implementations, it may be faster than eqv?, with the understanding that it sometimes returns false for equal numbers or characters. For other implementations, it's exactly identical to eqv?. Most of the time, for robustness, you should stick to using eqv? for doing identity comparisons, and forget that eq? exists.)

like image 71
Chris Jester-Young Avatar answered Oct 13 '22 23:10

Chris Jester-Young