0

Is there a trie implementation in sml that takes a number from input , then breaks up the number to its digits and inserts them in the trie? Is it an already developed structure or is there some already existing code which I could use? Does it have a lookup function ?

1 Answers1

1

Doing a quick search for "standard ml trie" gives the following two top results:

  1. github.com/cannam/sml-trie which isn't written in a way that aids educational purposes. The meat of the implementation is in the TrieMapFn functor which parameterises both the key and the element types, so you can specialise this to some number type.

  2. github.com/jlao/sml-trie which is a single file that is considerably easier to digest. This implementation assumes that keys are strings. You could either a) adapt this to use numbers instead -- it does, after all, explode the strings, so in the structure's internals are just lists of char, which might as well be lists of int -- or b) build a wrapper around this that converts a number (assuming that your numbers have some canonical string representation) into string before insertion and lookup.

For example, if your numbers were int:

(* Include jlao/sml-trie here. *)

signature NUMBER_DICT =
sig
  type key = int            (* concrete *)
  type 'a entry = key * 'a  (* concrete *)
  type 'a dict              (* abstract *)

  val empty : 'a dict
  val lookup : 'a dict -> key -> 'a option
  val insert : 'a dict * 'a entry -> 'a dict
  val toString : ('a -> string) -> 'a dict -> string
end

structure NumberTrie :> NUMBER_DICT =
struct
  type key = int
  type 'a entry = key * 'a
  type 'a dict = 'a Trie.dict  (* a string-based DICT *)

  val empty = Trie.empty
  fun lookup trie key = Trie.lookup trie (Int.toString key)
  fun insert (trie, (key, value)) = Trie.insert (trie, (Int.toString key, value))
  val toString = Trie.toString
end

Since jlao's DICT has a concrete key type, you have to make a new signature for each structure that changes the key type. This is not so generic and invites for us to convert this Trie structure into a functor that takes as parameter the key type. If your goal is to investigate building functors in the module system, this may be preferable.

sshine
  • 15,635
  • 1
  • 41
  • 66
  • Thank you for your answer. "This is not so generic and invites for us to convert this Trie structure into a functor that takes as parameter the key type" How can I do this? On a separate note ,what would you use as a key? since the trie will contain the number broken up and each digit will go into the node, what would be useful as a key? –  Jun 21 '19 at 08:33
  • Also, when I try to compile the code in the first link , I get the error unbound signature : PATTERN_MATCH_TRIE_MAP . Is there something wrong with the code? or am I doing something wrong? –  Jun 21 '19 at 09:13
  • @VassA: 1) You can read about functors in [ML for the Working Programmer, 2nd Ed. chapter 7](https://www.cl.cam.ac.uk/~lp15/MLbook/pub-details.html) (free PDF). 2) What I would use as a key depends on what problem I'm trying to solve. 3) I don't know if something is wrong with the code, I just googled it and mentioned that it was probably hard to use because it makes heavy use of functors and splits the code in abstract parts in multiple files, which was why I recommended the second result. – sshine Jun 21 '19 at 11:05