On Jan 3, 2:50 pm, Chouser <chou...@gmail.com> wrote:
> (def v1 [1 2])
> (def l1 '(1 2))
...
> But as noted originally, hashes are a bit more strict than Clojure =:
>
> (= v1 l1)  ==> true
> (get (hash-map v1 :found) l1)  ==> nil
>
> You can see why by calling the 'hash' function directly:
>
> (hash v1)  ==> 994
> (hash l1)  ==> -1919631597

OK, I understand all of that.  I still claim this is a bug.  Clojure
hashes are allowed to be more strict than Clojure =, I guess, although
I would very much dislike this state of affairs.

However, it is a Java *contract* [1] that
(.equals x y) ==> (== (.hashCode x) (.hashCode y)) and currently
Clojure data structures violate this contract:

user> (doseq [s ['(1 2) (seq '(1 2)) [1 2] (seq [1 2]) (ArrayList. [1
2])]]
  (print "\n" (.hashCode s))
  (doseq [t ['(1 2) (seq '(1 2)) [1 2] (seq [1 2]) (ArrayList. [1
2])]]
    (print "\t" (.equals s t))))

 -1919631597     true    true    true    true    false
 -1919631597     true    true    true    true    false
 994             true    true    true    true    true
 -1919631597     true    true    true    true    false
 994             false   false   true    false   true

IMHO, Clojure seqs/lists should use the java.util.List hash and
equality rules [2] so that the above matrix contains only "994" and
"true".

If the hash codes are left the same, the only other consistent option
is to change the definition of .equals so we get

 -1919631597     true    true    false   true    false
 -1919631597     true    true    false   true    false
 994             false   false   true    false   true
 -1919631597     true    true    false   true    false
 994             false   false   true    false   true

If it's really by design to violate the Java .hashCode contract, I'd
be very interested in hearing why.

Cheers,
Jason


[1] http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#hashCode()
[2] http://java.sun.com/j2se/1.4.2/docs/api/java/util/List.html#hashCode()

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to