Last active
August 29, 2015 14:20
-
-
Save mjdominus/be1bc369bce77d112eb3 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 | |
newtype NestedMap k = NestedMap (Map.Map k (NestedMap k)) | |
instance (Show k) => Show (NestedMap k) | |
where | |
show (NestedMap m) = | |
if (Map.null m) | |
then "{}" | |
else listToString | |
(\e -> "{ " ++ e ++ ", ") | |
(\e -> e ++ ", ") | |
(\e -> e ++ " }" ) | |
(\e -> "{ " ++ e ++ " }") | |
$ map (\(k, v) -> (show k) ++ " => " ++ (show v)) (Map.assocs m) | |
-- f irst, m iddle, l ast, s ingle | |
listToString f m l s [] = "" | |
listToString f m l s [x] = s x | |
listToString f m l s (h:t) = (f h) ++ restToString m l t | |
where restToString m l [x] = l x | |
restToString m l (h1:h2:t) = (m h1) ++ restToString m l (h2:t) | |
toMap (NestedMap nm) = nm | |
emptyNM = NestedMap Map.empty | |
insertNM k submap (NestedMap nm) = NestedMap $ Map.insert k submap nm | |
addpath nm [] = nm | |
addpath nm (h:t) = | |
maybe | |
(insertNM h (addpath emptyNM t) nm) | |
(\submap -> (insertNM h (addpath submap t) nm)) | |
(Map.lookup h $ toMap nm) | |
addpaths :: (Ord k) => (NestedMap k) -> [[k]] -> (NestedMap k) | |
addpaths = foldl addpath | |
hashpaths :: (Ord k) => [[k]] -> (NestedMap k) | |
hashpaths = addpaths emptyNM | |
example = hashpaths [["(X = 0)", "(y = True)", "(z = 0)"], | |
["(X = 0)", "(y = True)", "(z = S 0)"], | |
["(X = 0)", "(y = False)", "(z = S 0)"], | |
["(X = S 0)", "(y = True)", "(z = 0)"], | |
["(X = S 0)", "(y = False)", "(z = S 0)"] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most of this is window dressing. The
addpath
function is the important one.