Make things easier on yourself by separating concerns. E.g., each line encodes a position in its amount of leading whitespace followed by an identifier. Parse this representation into a more manipulable form in a single function invocation, don't thread it through your algorithm logic which shouldn't care about serialized representation. type SerializedGraph = String -- Aliases for pedagogical clarity type Indentation = Int type Symbol = String parse :: SerializedGraph -> [(Indentation, Symbol)] parse = map (first length . span (== ' ')) where first :: (a -> b) -> (a, c) -> (b, c) first f (a, c) = (f a, c) Now consider intermediate representations that can get you closer to your goal. Remember that it's easy to construct a map from an association list with `fromList`, so let's make that our end goal. Write out the types for a roadmap from where we are to where we want to be. [(Indentation, Symbol)] -- [(Hint, Key)] ==> ??? ==> [(Symbol, [Symbol])] -- [(Key, [Value])] If that final representation has only unique keys, then a reasonable intermediary would be one that hasn't had values which share a key accumulated yet. I.e., `[(Symbol, Symbol)] -- [(Key, Value)]`. associate :: [(Int, String)] -> [(String, String)] associate [] = [] associate ((i, s):ss) = [(s, t) | (j, t) <- descendants, j == i + 1] ++ associate descendants ++ associate siblings where (descendants, siblings) = span ((> i) . fst) ss Now all that's left to do is create a `Map`. We could accumulate values on our own, but really that's what `fromListWith` is for. toMap :: [(String, String)] -> Map String [String] toMap = fromListWith (++) . map (second (:[])) where second f (a, b) = (a, f b) All that's left is composing these functions together. readGraph :: String -> Map String [String] readGraph = toMap . associate . parse