Skip to content

Instantly share code, notes, and snippets.

@iokasimov
Last active October 15, 2017 18:35
Show Gist options
  • Save iokasimov/7fcf82f4ffd42813478bbf94f0e8a3f5 to your computer and use it in GitHub Desktop.
Save iokasimov/7fcf82f4ffd42813478bbf94f0e8a3f5 to your computer and use it in GitHub Desktop.
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