{-# 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