hashtable.sml 712 B

12345678910111213141516
  1. (* 1: Narrow down the type of the hashtable by adding a type contraint.
  2. * 2: Call `HashTable.mkTable` with 2 arguments:
  3. * 2.1: A hash function `HashString.hashString` and
  4. * 2.2: an equality function `op=`.
  5. * 3: Call the resulting function with 2 arguments:
  6. * 3.1: an initial size of the hash table and
  7. * 3.2: an exception to be thrown, when a key cannot be found in the hashtable and returning Option is not appropriate.
  8. *)
  9. val ht : (string, int) HashTable.hash_table =
  10. HashTable.mkTable (HashString.hashString, op=) (42, Fail "not found");
  11. HashTable.insert ht ("a", 1);
  12. HashTable.insert ht ("b", 2);
  13. HashTable.find ht "c"; (* -> Option *)
  14. HashTable.remove ht "c"; (* -> Exception *)