guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Comparing two hash tables for equality?


From: Mark H Weaver
Subject: Re: Comparing two hash tables for equality?
Date: Tue, 28 Aug 2018 03:37:46 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi Aleksandar,

Aleksandar Sandic <address@hidden> writes:

>> An equality test on hash tables needs to know how to compare the keys
>> and how to compare the values.  There's no way to pass those additional
>> arguments to 'equal?', so it can't do that job.
> It has to compare the values, but not the keys.

There's an implicit equality test on keys every time you perform a hash
table lookup.  By using 'hash-ref' and 'hash-get-handle', you are
implicitly using 'equal?' to compare the keys in the hash table with the
key that you're asking to look up.

Guile's native hash tables are unusual in that they do not internally
keep track of which equality test on keys to use.  Instead, it is your
responsibility to consistently use the functions corresponding to same
equality test in all accesses to a given hash table.  As the Guile
manual states:

  Like the association list functions, the hash table functions come in
  several varieties, according to the equality test used for the keys.
  Plain ‘hash-’ functions use ‘equal?’, ‘hashq-’ functions use ‘eq?’,
  ‘hashv-’ functions use ‘eqv?’, and the ‘hashx-’ functions use an
  application supplied test.

     A single ‘make-hash-table’ creates a hash table suitable for use
  with any set of functions, but it’s imperative that just one set is
  then used consistently, or results will be unpredictable.

  
<https://www.gnu.org/software/guile/manual/html_node/Hash-Table-Reference.html>

So, your 'hash-table-equal?' predicate implicitly assumes that the hash
tables passed to it were populated using 'hash-set!' or
'hash-create-handle!'.  If it is applied to a hash table that was
populated with the 'hashq-*!', 'hashv-*!', or 'hashx-*!' procedures,
then the results will be unpredictable.

My 'hash-table=?' predicate makes the same implicit assumption.

      Mark



reply via email to

[Prev in Thread] Current Thread [Next in Thread]