Is eqv? the portable way of checking object equality?

3 views
Skip to first unread message

Grant Rettke

unread,
Apr 19, 2009, 8:40:48 PM4/19/09
to
From what I have read, eq? is a way of determing whether two objects
are the same based on whether they point to the same location in
memory; and generally that eqv? works the same way as eq? but for
numbers and characters; numbers and characters equality will not be
determined consistently across different implementations. Additionally
I have read that as a result of this one ought to use eqv? as a
portable means for checking for object equality unless performance is
such an issue that eq? should be used instead.

Is eqv? the portable way of checking object equality?

Grant Rettke

unread,
Apr 19, 2009, 8:44:57 PM4/19/09
to

William D Clinger

unread,
Apr 19, 2009, 9:31:04 PM4/19/09
to
Grant Rettke wrote:
> From what I have read, eq? is a way of determing whether two objects
> are the same based on whether they point to the same location in
> memory; and generally that eqv? works the same way as eq? but for
> numbers and characters; numbers and characters equality will not be
> determined consistently across different implementations.

There are a few other differences as well, but numbers
and characters are indeed the most commonly encountered.

> Additionally
> I have read that as a result of this one ought to use eqv? as a
> portable means for checking for object equality unless performance is
> such an issue that eq? should be used instead.

That's reasonable, modulo the quibble with "object
equality" below.

> Is eqv? the portable way of checking object equality?

Yes to the first half of that sentence, provided you
replace "portable" by "most portable standard".

As for the second half of the sentence, I have this
quibble: In general, there is no such thing as *the*
notion of object equality; the appropriate notion of
object equality depends upon the context. In particular,
it depends on whether you want to compare two objects
with respect to mutability (that is, does a side
effect to one object imply the same side effect to
the other?) or with respect to their current state.

In Scheme (whether IEEE/ANSI/R5RS or R6RS), eqv? is
the most implementation-independent of the pre-defined
equivalence predicates that are defined on all objects
and are guaranteed to distinguish two distinct mutable
objects. Similarly, equal? is the pre-defined partial
equivalence predicate that distinguishes on the basis
of current state but not on the basis of mutation; the
R6RS equal? is also total.

In Scheme, eq? is basically an efficiency hack that
can be used as an alternative to eqv? when you know
for sure that at least one of the arguments is a
boolean, symbol, empty list, pair, procedure, non-empty
string, non-empty vector, non-empty bytevector (R6RS
only), or record (R6RS only). The rationale for eq?
is that it is often about ten times as fast as eqv?,
which is enough to matter for some applications, and
the specific list of situations for which eq? is
guaranteed to behave the same as eqv? is about as
inclusive as it can be without sacrificing speed in
typical implementations.

Will

Mr.Cat

unread,
Apr 20, 2009, 6:07:48 AM4/20/09
to
On Apr 20, 4:40 am, Grant Rettke <gret...@gmail.com> wrote:
> Is eqv? the portable way of checking object equality?


Depending on whar kind of objects you're dealing with. I.e. (eqv? 1
1.0) is often #f.

Grant Rettke

unread,
Apr 20, 2009, 2:41:37 PM4/20/09
to

For inexact and exact numbers, =. That is the normal way to check
isn't it?

Mr.Cat

unread,
Apr 20, 2009, 4:49:07 PM4/20/09
to
On Apr 20, 10:41 pm, Grant Rettke <gret...@gmail.com> wrote:
> For inexact and exact numbers, =. That is the normal way to check
> isn't it?

Iirc it is.
I just wanted to say that eqv?/equal? do not always behave as one
expects `the portable way of checking object equality' to behave. As
for me, I was quite surprised when I first ran into in-equal?-ity of
an inexact number and its exact counterpart.

Reply all
Reply to author
Forward
0 new messages