Skip to content

Instantly share code, notes, and snippets.

@gnuvince
Created March 8, 2013 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gnuvince/5113842 to your computer and use it in GitHub Desktop.
Save gnuvince/5113842 to your computer and use it in GitHub Desktop.
module CMap = Map.Make(struct
type t = char
let compare = Char.compare
end)
type t = Trie of (t CMap.t * bool)
let empty = Trie (CMap.empty, false)
let add word trie =
let rec loop i (Trie (m, eow)) =
if i = String.length word then
Trie (m, true)
else
try
let trie' = CMap.find word.[i] m in
Trie (CMap.add word.[i] (loop (i+1) trie') m, eow)
with Not_found ->
Trie (CMap.add word.[i] (loop (i+1) empty) m, eow)
in
loop 0 trie
let rec mem word trie =
let rec loop i (Trie (m, eow)) =
if i = String.length word then
eow
else
try
let trie' = CMap.find word.[i] m in
loop (i+1) trie'
with Not_found ->
false
in
loop 0 trie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment