开发者

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 开发者_JS百科quoted symbols?


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.)


From Scheme Documentation https://www.gnu.org/software/guile/manual/html_node/Equality.html

eq? tests just for the same object (essentially a pointer comparison). This is fast, and can be used when searching for a particular object, or when working with symbols or keywords (which are always unique objects).

eqv? extends eq? to look at the value of numbers and characters. It can for instance be used somewhat like = (see Comparison) but without an error if one operand isn’t a number.

equal? goes further, it looks (recursively) into the contents of lists, vectors, etc. This is good for instance on lists that have been read or calculated in various places and are the same, just not made up of the same pairs. Such lists look the same (when printed), and equal? will consider them the same.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜