[plt-scheme] Switching to Scheme
Mark Engelberg wrote:
> I understand that eq? is the fastest way to compare, but it offers
> very little guarantee that it makes meaningful comparisons. According
> to the standard, there's even no guarantee that (eq? 2 2) will return
> true. So which things are safe to use as keys for the eq? style
> hashtable? Can strings and/or lists be easily converted into
> something that can be compared with eq?
Someone might catch me out as being wrong, but a naive test yields the
following:
Definitions:
------------
(eq? 'a 'a)
(define a-sym 'a)
(define a-sym2 'a)
(eq? a-sym a-sym2)
(eq? 'a (string->symbol "a"))
(eq? 'a (string->symbol (format "~a" a-sym)))
Interactions:
-------------
Welcome to DrScheme, version 350.
Language: Textual (MzScheme, includes R5RS).
#t
#t
#t
#t
>
FWIW, I tend to use symbols and integers for keys. You can get from
strings to symbols with 'string->symbol', and from lists to symbols
with... (string->symbol (list->string <ls>)). The latter looks expensive
in the long run. If you're able to, just use symbols in the first place.
I've often used integers for keys, but perhaps someone can tell us
whether this is a good idea or not for hash tables using 'eq?' as an
equality test.
M