Search code examples
structurehashtablesmlsmlnj

SML/NJ: How to use HashTable?


I really want to create a HashTable in SML, it seems there already is a structure for this in SML/NJ.

The question is, how do I use it? I've not fully understood how to use structures in SML, and some of the very basic examples in the book I read gives me errors I don't even know how to correct, so using the HashTable structure might be an easy thing, but I wouldn't know. If someone could explain this, then that'd be wonderful too!

I'm thinking it's something like this:

val ht : string * int HashTable.hash_table = HashTable.mkTable();

???


Solution

  • The signature of the mkTable value is:

    val mkTable : (('a -> word) * (('a * 'a) -> bool)) -> (int * exn)
          -> ('a,'b) hash_table
        (* Given a hashing function and an equality predicate, create a new table;
         * the int is a size hint and the exception is to be raised by find.
         *)
    

    Therefore, you would have to do something like:

    val ht : (string, int) HashTable.hash_table =
        HashTable.mkTable (HashString.hashString, op=) (42, Fail "not found")