Arc Forumnew | comments | leaders | submitlogin
2 points by rocketnia 5756 days ago | link | parent

Quick: is the key a table value or reference?

It's a reference, as you can see, since the keys in the table change when you change them from outside the table. It's just that the keys are being compared by structure rather than by reference.

(tab key) => nil

This is pretty much a casualty of using hash tables. The point of a hash table is that it redundantly stores a hash of each of the keys, then compares further keys' hashes to those hashes before doing more in-depth comparisons. If you modify a key, then the hash table's precalculated hash for that key may be incorrect, so the shortcut doesn't work properly anymore.

The PLT Scheme docs specifically don't promise anything about this:

Caveat concerning mutable keys: If a key in an equal?-based hash table is mutated (e.g., a key string is modified with string-set!), then the hash table's behavior for insertion and lookup operations becomes unpredictable. (http://docs.plt-scheme.org/reference/hashtables.html)

I imagine a language could make sure to update its hash table hashes whenever the keys were modified, but as far as I can tell, that involves either recalculating the hash values at table access time (which defeats the purpose somewhat), recalculating them at modification time (which I'm guessing would make all modification take time proportional to the number of things that (in)directly reference the modified thing), or trying to do the recalculation sometime in between when there's time to spare, like the same time the garbage collector runs. There are probably some cleverer solutions, but any way you slice it, I doubt it's a straightforward fix. ^_^;



2 points by akkartik 5756 days ago | link

Ah, that's most illuminating, thanks! So that's why palsecam used an alist ^_^

-----