[plt-scheme] Another question: eq? and eqv?

From: Greg Woodhouse (gregory.woodhouse at sbcglobal.net)
Date: Fri Dec 30 20:06:16 EST 2005

--- Eli Barzilay <eli at barzilay.org> wrote:

> On Dec 30, Greg Woodhouse wrote:
> > I understand that eq? is supposed to compare objects for identity
> (like
> > comparing pointers) and equal? compares objects for value. I also
> > understand that equal? recursively applies eqv? to the components
> of a
> > structure. What is less clear to me is how eq? and eqv? differ.
> When is
> > (eq? a b) different from (eqv? a b) ?
> 
> See the R5RS text -- the bottom line is that `eq?' is comparing
> actual
> objects, so it is guaranteed to be constant time etc etc.  But it can
> also expose implementation details, like
> 
>   (eq? (expt 2 40) (expt 2 40))
> 
> is false in mzscheme if it was compiled on a 32-bit platform, and
> true
> on a 64-bit.  OTOH,
> 
>   (eqv? (expt 2 40) (expt 2 40))
> 
> will always return #t, since it's just an implementation detail that
> they are different.  Various Schemes can do all kinds of similarly
> crazy stuff, and `eqv?' is supposed to be more, um, reasonable.
> 
> -- 
>           ((lambda (x) (x x)) (lambda (x) (x x)))          Eli
> Barzilay:
>                   http://www.barzilay.org/                 Maze is
> Life!
> 

I've been experimenting with mutable and immutable strings, thinking
two immutable strings equal to "hello" might be eq? but mutable strings
would not be. No luck. But your example works. In fact, on the machine
I'm using right now (a Pentium-something or another) 29 works, but not
30.

> (define a (expt 2 29))
> (define b (expt 2 29))
> (eq? a b)
#t
> (define a (expt 2 30))
> (define b (expt 2 30))
> (eq? a b)
#f
> 


===
Gregory Woodhouse  <gregory.woodhouse at sbcglobal.net>
"Interaction is the mind-body problem of computing."
--Philip L. Wadler


Posted on the users mailing list.