From 618f258c51f6c018542d171d6c37c39cb40d428f Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Mon, 30 Jun 2025 18:40:12 +0200 Subject: add homework files for lecture 2 --- lec02/Log.hs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lec02/Log.hs (limited to 'lec02/Log.hs') 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 -- cgit v1.2.3