parse :: String -> [LogMessage]
parse s = map parseMessage (lines s)
+-- Exercise 2
+insert :: LogMessage -> MessageTree -> MessageTree
+insert (LogMessage t ts s) (Leaf) = Node Leaf (LogMessage t ts s) Leaf
+insert (LogMessage t1 ts1 s1) (Node lt (LogMessage t2 ts2 s2) rt) | ts1 <= ts2 = Node (insert (LogMessage t1 ts1 s1) lt) (LogMessage t2 ts2 s2) rt
+ | otherwise = Node lt (LogMessage t2 ts2 s2) (insert (LogMessage t1 ts1 s1) rt)
+insert _ tree = tree
+-- Exercise 3
+build :: [LogMessage] -> MessageTree
+build = foldr (\msg t -> insert msg t) Leaf
+
+-- Exercise 4
+inOrder :: MessageTree -> [LogMessage]
+inOrder (Leaf) = []
+inOrder (Node lt rootmsg rt) = inOrder lt ++ [rootmsg] ++ inOrder rt
+
+-- Exercise 5
+whatWentWrong :: [LogMessage] -> [String]
+whatWentWrong ls = let sortedLogMsgs = inOrder (build ls)
+ in
+ filterErrorMessages (>= 50) sortedLogMsgs
+ where
+ filterErrorMessages _ [] = [""]
+ filterErrorMessages f ((LogMessage (Error level) _ s):ms) | f level = s: filterErrorMessages f ms
+ | otherwise = filterErrorMessages f ms
+ filterErrorMessages f (_:ms) = filterErrorMessages f ms