[racket] hash table confusion

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Mon Dec 30 23:27:02 EST 2013

On Mon, 30 Dec 2013 15:40:08 -0600
Robby Findler <robby at eecs.northwestern.edu>
wrote:

> The hash tables seem fine to me, but maybe we need a better example.
> 
> #lang racket
> 
> (struct cpd (x y))
> 
> (define C1 (cpd 1 "bla"))
> (define C2 (cpd 1 "bla"))
> 
> (equal? C1 C2)
> #f
> 
> (define ht (make-hash))
> (hash-set! ht C1 1)
> (hash-set! ht C2 2)
> (equal? (hash-ref ht C1)
>         (hash-ref ht C2))
> ;; produces #f
> 
> 

Actually, it is all fine. In my real code I had used #:transparent and
the hash table code worked as expected. 

Then when reading that make-hash uses equal? I wasn't sure how equal?
does work with structs. My small test were without #:transparent, and I
didn't realize that exactly this makes the difference.

-- 
Thanks,
Manfred





> On Sat, Dec 28, 2013 at 5:34 PM, Sam Tobin-Hochstadt
> <samth at cs.indiana.edu>wrote:
> 
> > While your answer is what Manfred is looking for, I still find the
> > original behavior worrying. It lets you expose the internals of
> > opaque data structures without having the relevant inspector.
> >
> > Sam
> > On Dec 28, 2013 4:16 PM, "J. Ian Johnson"
> > <ianj at ccs.neu.edu> wrote:
> >
> >> You want to make sure your structs are inspectable. You can define
> >> your structs with the #:transparent option or define your own
> >> equality relation with the gen:equal+hash generic interface.
> >> -Ian
> >> ----- Original Message -----
> >> From: Manfred Lotz <manfred.lotz at arcor.de>
> >> To: users at racket-lang.org
> >> Sent: Sat, 28 Dec 2013 15:41:28 -0500 (EST)
> >> Subject: [racket] hash table confusion
> >>
> >> I have a hash table (created by using make-hash) where the key is a
> >> struct. This seems to work fine although the documentation says
> >>    The make-hash procedure creates a table where keys are compared
> >> with equal?,...
> >>
> >> and it seems that equal? gives #f if I compare two structs having
> >> the same contents:
> >>
> >>
> >> (struct cpd (x y))
> >>
> >> (define C1 (cpd 1 "bla"))
> >> (define C2 (cpd 1 "bla"))
> >>
> >> (equal? C1 C2)
> >> #f
> >>
> >>
> >> Why does my hash table work fine? In my hash handling I'm using
> >> make-hash, hash-ref!, hash-set!, hash-for-each.
> >>
> >> --
> >> Manfred
> >>
> >>
> >> ____________________
> >>   Racket Users list:
> >>   http://lists.racket-lang.org/users
> >>
> >> ____________________
> >>   Racket Users list:
> >>   http://lists.racket-lang.org/users
> >>
> >
> > ____________________
> >   Racket Users list:
> >   http://lists.racket-lang.org/users
> >
> >
> 



Posted on the users mailing list.