Last active
October 15, 2017 18:35
-
-
Save iokasimov/7fcf82f4ffd42813478bbf94f0e8a3f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.List | |
data Trie key value = Vertice value [Edge key value] deriving Show | |
data Edge key value = Edge key (Trie key value) deriving Show | |
example :: Trie Char Integer | |
example = Vertice 0 [ | |
Edge 'h' $ Vertice 0 [ | |
Edge 'e' $ Vertice 1 [], | |
Edge 'i' $ Vertice 0 [ | |
Edge 's' $ Vertice 3 [] | |
] | |
], | |
Edge 's' $ Vertice 0 [ | |
Edge 'h' $ Vertice 0 [ | |
Edge 'e' $ Vertice 2 [] | |
] | |
] | |
] | |
lookup' :: Eq a => [a] -> Trie a b -> Maybe b | |
lookup' [] (Vertice value edges) = Just value | |
lookup' (step:path) (Vertice _ edges) = find (\(Edge k v) -> k == step) edges >>= | |
\(Edge key vertice) -> lookup' path vertice | |
main = print $ lookup' "his" example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment