Fix combination bug

This commit is contained in:
2021-12-06 02:33:36 +00:00
parent 22e3256da3
commit 40195d033c
12 changed files with 122 additions and 100 deletions
+26
View File
@@ -0,0 +1,26 @@
{-# LANGUAGE TemplateHaskell #-}
module SimpleTrie where
import Data.Map
import Control.Lens
data Trie a b = Trie
{ _trieMVal :: Maybe b
, _trieChildren :: Map a (Trie a b)
}
makeLenses ''Trie
emptyTrie :: Trie a b
emptyTrie = Trie Nothing empty
singletonTrie :: Ord a => [a] -> b -> Trie a b
singletonTrie (k:ks) x = Trie Nothing $ singleton k $ singletonTrie ks x
singletonTrie [] x = Trie (Just x) empty
insertInTrie :: Ord a => [a] -> b -> Trie a b -> Trie a b
insertInTrie [] x = trieMVal .~ Just x
insertInTrie (k:ks) x = trieChildren %~ insertWith f k (singletonTrie ks x)
where
f _ = insertInTrie ks x
lookupTrie :: Ord a => [a] -> Trie a b -> Maybe b
lookupTrie [] t = _trieMVal t
lookupTrie (k:ks) t = _trieChildren t !? k >>= lookupTrie ks