From c4cdf98f4a40b1544961b919206eb09366081e32 Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
Date: Tue, 16 Dec 2014 11:42:50 +0530
Subject: [PATCH] hw #2, done

---
 hw2/LogAnalysis.hs | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/hw2/LogAnalysis.hs b/hw2/LogAnalysis.hs
index dc393b2..d494a25 100644
--- a/hw2/LogAnalysis.hs
+++ b/hw2/LogAnalysis.hs
@@ -15,4 +15,29 @@ parseMessage s = let ws = words s in
 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
-- 
2.45.2