unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* doc eq? eqv? equal?
@ 2004-08-20  1:26 Kevin Ryde
  2004-08-20 10:26 ` Marius Vollmer
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2004-08-20  1:26 UTC (permalink / raw)


I'm looking to polish the docs for eq?, eqv? and equal? a bit.  r5rs
says numbers are unspecified for eq?, even going so far as to say

	(let ((n (+ 2 3)))
	  (eq? n n))       => *unspecified*

I guess that's meant to allow something wild and strange to represent
numbers.

In guile "(eq? x x)" is always going to be true, but I'm wondering if
the docs should describe only the weaker r5rs behaviour, and advise
eqv? for anything involving numbers.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: doc eq? eqv? equal?
  2004-08-20  1:26 doc eq? eqv? equal? Kevin Ryde
@ 2004-08-20 10:26 ` Marius Vollmer
  2004-08-22  1:54   ` Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Marius Vollmer @ 2004-08-20 10:26 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> In guile "(eq? x x)" is always going to be true, but I'm wondering if
> the docs should describe only the weaker r5rs behaviour, and advise
> eqv? for anything involving numbers.

Yes, comparing x with itself is probably rare and the general case
(eq? x y) is false for bignums.  So eqv? is the right thing to use for
numbers.  Or '=' if you want to make sure that you are dealing with
numbers.

    guile> (eq? (expt 2 20) (expt 2 20))
    #t
    guile> (eq? (expt 2 40) (expt 2 40))
    #f
    guile> (eqv? (expt 2 40) (expt 2 40))
    #t
    guile> (= (expt 2 40) (expt 2 40))
    #t


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: doc eq? eqv? equal?
  2004-08-20 10:26 ` Marius Vollmer
@ 2004-08-22  1:54   ` Kevin Ryde
  2004-08-24 17:24     ` Marius Vollmer
  0 siblings, 1 reply; 5+ messages in thread
From: Kevin Ryde @ 2004-08-22  1:54 UTC (permalink / raw)
  Cc: guile-devel

Marius Vollmer <marius.vollmer@uni-dortmund.de> writes:
>
> Yes, comparing x with itself is probably rare and the general case
> (eq? x y) is false for bignums.

Yep, but it was the case (eq? x x) I was wondering about.  r5rs
doesn't guarantee it's always true, but in guile it will be.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: doc eq? eqv? equal?
  2004-08-22  1:54   ` Kevin Ryde
@ 2004-08-24 17:24     ` Marius Vollmer
  2004-09-02  1:24       ` Kevin Ryde
  0 siblings, 1 reply; 5+ messages in thread
From: Marius Vollmer @ 2004-08-24 17:24 UTC (permalink / raw)


Kevin Ryde <user42@zip.com.au> writes:

> Marius Vollmer <marius.vollmer@uni-dortmund.de> writes:
>>
>> Yes, comparing x with itself is probably rare and the general case
>> (eq? x y) is false for bignums.
>
> Yep, but it was the case (eq? x x) I was wondering about.  r5rs
> doesn't guarantee it's always true, but in guile it will be.

I'd say it is OK to say nothing about this.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: doc eq? eqv? equal?
  2004-08-24 17:24     ` Marius Vollmer
@ 2004-09-02  1:24       ` Kevin Ryde
  0 siblings, 0 replies; 5+ messages in thread
From: Kevin Ryde @ 2004-09-02  1:24 UTC (permalink / raw)


New words about eq? etc.

I dropped the bit in eqv about "left slightly open to interpretation",
which doesn't really seem right in my reading of the r5rs.  My reading
is that literals and indistinguishable procedures may or may not be
eqv, but that's the limit of the flexibility, at least as far as the
standard types go.



3.1.2 Equality
--------------

There are three kinds of core equality predicates in Scheme, described
below.  The same kinds of comparison arise in other functions, like
`memq' and friends (*note List Searching::).

   For all three tests, objects of different types are never equal.  So
for instance a list and a vector are not `equal?', even if their
contents are the same.  Exact and inexact numbers are considered
different types too, and are hence not equal even if their values are
the same.

   `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 into the value of numbers and
characters.  It can for instance be used somewhat like `=' (*note
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.


 -- Scheme Procedure: eq? x y
 -- C Function: scm_eq_p (x, y)
     Return `#t' if X and Y are the same object, except for numbers and
     characters.  For example,

          (define x (vector 1 2 3))
          (define y (vector 1 2 3))

          (eq? x x)  => #t
          (eq? x y)  => #f

     Numbers and characters are not equal to any other object, but the
     problem is they're not necessarily `eq?' to themselves either.
     This is even so when the number comes directly from a variable,

          (let ((n (+ 2 3)))
            (eq? n n))       => *unspecified*

     Generally `eqv?' below should be used when wanting to compare
     numbers or characters.  `=' (*note Comparison::) or `char=?'
     (*note Characters::) can be used too.

     It's worth noting that end-of-list `()', `#t', `#f', a symbol of a
     given name, and a keyword of a given name, are unique objects.
     There's just one of each, so for instance no matter how `()'
     arises in a program, it's the same object and can be compared with
     `eq?',

          (define x (cdr '(123)))
          (define y (cdr '(456)))
          (eq? x y) => #t

          (define x (string->symbol "foo"))
          (eq? x 'foo) => #t

 -- C Function: int scm_is_eq (SCM x, SCM y)
     Return `1' when X and Y are equal in the sense of `eq?', otherwise
     return `0'.

     The `==' operator should not be used on `SCM' values, an `SCM' is
     a C type which cannot necessarily be compared using `==' (*note
     The SCM Type::).


 -- Scheme Procedure: eqv? x y
 -- C Function: scm_eqv_p (x, y)
     Return `#t' if X and Y are the same object, or for characters and
     numbers the same value.

     On objects except characters and numbers, `eqv?' is the same as
     `eq?' above, it's true if X and Y are the same object.

     If X and Y are numbers or characters, `eqv?' compares their type
     and value.  An exact number is not `eqv?' to an inexact number
     (even if their value is the same).

          (eqv? 3 (+ 1 2)) => #t
          (eqv? 1 1.0)     => #f


 -- Scheme Procedure: equal? x y
 -- C Function: scm_equal_p (x, y)
     Return `#t' if X and Y are the same type, and their contents or
     value are equal.

     For a pair, string, vector or array, `equal?' compares the
     contents, and does so using using the same `equal?' recursively,
     so a deep structure can be traversed.

          (equal? (list 1 2 3) (list 1 2 3))   => #t
          (equal? (list 1 2 3) (vector 1 2 3)) => #f

     For other objects, `equal?' compares as per `eqv?' above, which
     means characters and numbers are compared by type and value (and
     like `eqv?', exact and inexact numbers are not `equal?', even if
     their value is the same).

          (equal? 3 (+ 1 2)) => #t
          (equal? 1 1.0)     => #f

     Hash tables are currently only compared as per `eq?', so two
     different tables are not `equal?', even if their contents are the
     same.

     `equal?' does not support circular data structures, it may go into
     an infinite loop if asked to compare two circular lists or similar.

     New application-defined object types (*note Defining New Types
     (Smobs)::) have an `equalp' handler which is called by `equal?'.
     This lets an application traverse the contents or control what is
     considered `equal?' for two objects of such a type.  If there's no
     such handler, the default is to just compare as per `eq?'.



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-02  1:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-20  1:26 doc eq? eqv? equal? Kevin Ryde
2004-08-20 10:26 ` Marius Vollmer
2004-08-22  1:54   ` Kevin Ryde
2004-08-24 17:24     ` Marius Vollmer
2004-09-02  1:24       ` Kevin Ryde

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).