Re: 4.0.0 problem with using symbols as hash table keys

From: Jonathan Berry <berryj_at_elon.edu>
Date: Sun, 12 Sep 1999 15:31:58 -0400 (EDT)

Thanks to Shiro for the suggestion, but it still doesn't work:
-------------------------------------------------------------------
Welcome to the STk interpreter version 4.0.0 [Linux-2.X-ix86]
Copyright ) 1993-1999 Erick Gallesio - I3S - CNRS / ESSI <eg_at_unice.fr>
STk> (define (my-hash v)
        (display "my-hash: ")
        (display v) (newline)
        (display "hashes to: ")
        (if (symbol? v) (display (hash-table-hash (symbol->string v)))
                         (display (hash-table-hash v)))
        (newline)
        (if (symbol? v) (hash-table-hash (symbol->string v))
                         (hash-table-hash v)))

my-hash
STk> (define h (make-hash-table equal? my-hash))
h
STk> (define s (gensym "abc"))
s
STk> (hash-table-put! h s 5)
my-hash: abc18
hashes to: 716375
STk> (hash-table-get h s)
my-hash: abc18
hashes to: 716375
5
STk> (hash-table-get h 'abc18)
my-hash: abc18
hashes to: 716375
5
STk> ; so far so good, but here's my problem:

(define g (gensym "graph-view"))
g
STk> g
graph-view19
STk> (hash-table-put! h g 6)
my-hash: graph-view19
hashes to: 475627620
STk> (hash-table-get h g)
my-hash: graph-view19
hashes to: 475627620

*** Error:
hash-table-get: entry not defined for this key: graph-view19
STk> (hash-table-get h 'graph-view19)
my-hash: graph-view19
hashes to: 475627620

*** Error:
hash-table-get: entry not defined for this key: graph-view19
STk> (hash-table->list h)
((abc18 . 5) (graph-view19 . 6))
STk>
-----------------------------------------------------------------
        -Jon

On Sat, 11 Sep 1999, Shiro Kawai wrote:

> I guess this is actually a better behavior, because the symbol
> returned by gensym is not interned so that it can't be eq? to
> the symbols with the same name. The default hash function uses
> a pointer of the symbol to calculate the hash value, which prevents
> you from looking up by the quoted symbol.
>
> The confusing point is that the symbol from gensym and the quoted
> symbol can be equal?-equivalent. Is that a feature?
>
> The workaround may be to define your own hash function which uses
> symbol name to calculate hash value, like this:
>
> STk> (define (my-hash v)
> (if (symbol? v)
> (hash-table-hash (symbol->string v))
> (hash-table-hash v)))
> my-hash
> STk> (define s (gensym "abc"))
> s
> STk> s
> abc2
> STk> (my-hash s)
> 79592
> STk> (my-hash 'abc2)
> 79592
> STk> (define h (make-hash-table equal? my-hash))
> h
> STk> (hash-table-put! h s 6)
> STk> (hash-table-get h s)
> 6
> STk> (hash-table-get h 'abc2)
> 6
> STk>
>
> --
> Shiro KAWAI
> Square USA Inc. Honolulu Studio R&D division
> #"The most important things are the hardest things to say" --- Stephen King
>
>
Received on Sun Sep 12 1999 - 21:37:22 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST