summaryrefslogtreecommitdiff
path: root/lec02/Log.hs
diff options
context:
space:
mode:
authorEdoardo La Greca2025-06-30 18:40:12 +0200
committerEdoardo La Greca2025-06-30 18:40:12 +0200
commit618f258c51f6c018542d171d6c37c39cb40d428f (patch)
tree462f9c8b745a37c4495bec85c8df9917d314ef76 /lec02/Log.hs
parenta359fbae1b7e7ec210cf537370126435ceb4d4d0 (diff)
add homework files for lecture 2
Diffstat (limited to 'lec02/Log.hs')
-rw-r--r--lec02/Log.hs38
1 files changed, 38 insertions, 0 deletions
diff --git a/lec02/Log.hs b/lec02/Log.hs
new file mode 100644
index 0000000..dce9367
--- /dev/null
+++ b/lec02/Log.hs
@@ -0,0 +1,38 @@
+-- CIS 194 Homework 2
+
+module Log where
+
+import Control.Applicative
+
+data MessageType = Info
+ | Warning
+ | Error Int
+ deriving (Show, Eq)
+
+type TimeStamp = Int
+
+data LogMessage = LogMessage MessageType TimeStamp String
+ | Unknown String
+ deriving (Show, Eq)
+
+data MessageTree = Leaf
+ | Node MessageTree LogMessage MessageTree
+ deriving (Show, Eq)
+
+-- | @testParse p n f@ tests the log file parser @p@ by running it
+-- on the first @n@ lines of file @f@.
+testParse :: (String -> [LogMessage])
+ -> Int
+ -> FilePath
+ -> IO [LogMessage]
+testParse parse n file = take n . parse <$> readFile file
+
+-- | @testWhatWentWrong p w f@ tests the log file parser @p@ and
+-- warning message extractor @w@ by running them on the log file
+-- @f@.
+testWhatWentWrong :: (String -> [LogMessage])
+ -> ([LogMessage] -> [String])
+ -> FilePath
+ -> IO [String]
+testWhatWentWrong parse whatWentWrong file
+ = whatWentWrong . parse <$> readFile file