blob: 0c9e714609792ef9492cdd44250ddb83dd54d298 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where
import Log
-- Exercise 1
nthWord :: Int -> String -> String
nthWord n = (!! n) . words
firstWord :: String -> String
firstWord = nthWord 0
nthWordInt :: Int -> String -> Int
nthWordInt n = read . nthWord n
timeStamp :: String -> Int
timeStamp s
| firstWord s == "E" = nthWordInt 2 s
| otherwise = nthWordInt 1 s
restOfMessage :: String -> String
restOfMessage s
| firstWord s == "E" = (unwords . drop 3 . words) s
| otherwise = (unwords . drop 2 . words) s
parseMessage :: String -> LogMessage
parseMessage s
| firstWord s == "I" = LogMessage Info (timeStamp s) (restOfMessage s)
| firstWord s == "W" = LogMessage Warning (timeStamp s) (restOfMessage s)
| firstWord s == "E" = LogMessage (Error (nthWordInt 1 s)) (timeStamp s) (restOfMessage s)
| otherwise = Unknown s
parse :: String -> [LogMessage]
parse = map parseMessage . lines
-- Exercise 2
insert :: LogMessage -> MessageTree -> MessageTree
insert (Unknown _) t = t
insert _ t@(Node _ (Unknown _) _) = t -- should never happen
insert m Leaf = Node Leaf m Leaf
insert m@(LogMessage _ newTime _) (Node tl (LogMessage _ time _) tr)
| newTime < time = insert m tl
| otherwise = insert m tr
|