summaryrefslogtreecommitdiff
path: root/lec02/LogAnalysis.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-06-30 20:13:59 +0200
committerEdoardo La Greca2025-06-30 20:13:59 +0200
commit8ee84f2253a9311fb365d994012aa6de06ef6b92 (patch)
treea7b68f87ce3c4faae6ace1e418048ccc2cd4c742 /lec02/LogAnalysis.hs
parent618f258c51f6c018542d171d6c37c39cb40d428f (diff)
add first exercise of lecture 2
Diffstat (limited to 'lec02/LogAnalysis.hs')
-rw-r--r--lec02/LogAnalysis.hs35
1 files changed, 35 insertions, 0 deletions
diff --git a/lec02/LogAnalysis.hs b/lec02/LogAnalysis.hs
new file mode 100644
index 0000000..fc4d3df
--- /dev/null
+++ b/lec02/LogAnalysis.hs
@@ -0,0 +1,35 @@
+{-# 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