From: Ramakrishnan Muthukrishnan Date: Tue, 16 Dec 2014 06:12:50 +0000 (+0530) Subject: hw #2, done X-Git-Url: https://git.rkrishnan.org/tahoe_css?a=commitdiff_plain;h=c4cdf98f4a40b1544961b919206eb09366081e32;p=yorgey.git hw #2, done --- 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