Last active
May 16, 2016 08:08
-
-
Save poetix/5761dda930c5c1eb16552d0bc923e68e 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 qualified Data.Map as Map | |
type SubPaths = Map.Map String Node | |
data Node = Content SubPaths | NoContent SubPaths deriving (Show) | |
content :: [(String, Node)] -> Node | |
content kvs = Content $ Map.fromList kvs | |
nocontent :: [(String, Node)] -> Node | |
nocontent kvs = NoContent $ Map.fromList kvs | |
routes :: Node | |
routes = content [ | |
("animal", nocontent [ | |
("bird", content []), | |
("mammal", nocontent [ | |
("cat", content []), | |
("dog", content [])])]), | |
("vegetable", nocontent [ | |
("flower", nocontent [ | |
("daisy", content [])]), | |
("tree", nocontent [ | |
("oak", nocontent [])])]), | |
("mineral", content [ | |
("rock", nocontent [])])] | |
joinPath :: String -> String -> String | |
joinPath path key = path ++ ('/' : key) | |
getSubPaths :: SubPaths -> String -> [String] | |
getSubPaths subpaths path = do | |
(key, subnode) <- Map.assocs subpaths | |
getPaths' subnode $ joinPath path key | |
getPaths' :: Node -> String -> [String] | |
getPaths' (Content subpaths) path = path : getSubPaths subpaths path | |
getPaths' (NoContent subpaths) path = getSubPaths subpaths path | |
getPaths :: Node -> [String] | |
getPaths node = getPaths' node "" | |
main :: IO () | |
main = do | |
mapM_ putStrLn $ getPaths routes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment