Super new to Haskell and I’m loving the language.
I’m doing the homework exercises in CIS194, particularly, Homework 2 - Exercise 2,
Lecture 2 provides Log.hs and I am to write LogAnalysis.hs
Log.hs
-- 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
I already accomplished exercise 1
{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where
import Log
-- Exercise 1
parseMessage :: String -> LogMessage
parseMessage strLog = case words strLog of
("I" : ts : logString) ->
LogMessage Info (read ts) (unwords logString)
("W" : ts : logString) ->
LogMessage Warning (read ts) (unwords logString)
("E" : etype : ts : logString) ->
LogMessage (Error (read etype)) (read ts) (unwords logString)
_ -> Unknown strLog
parse :: String -> [LogMessage]
parse strLogs = map parseMessage (lines strLogs)
and now for exercise 2, i am to insert LogMessage
nodes to a MessageTree
binary search tree
this is my implementation currently
-- Exercise 2
insert :: LogMessage -> MessageTree -> MessageTree
insert (Unknown _) msgTree = msgTree
insert logMsg@(LogMessage{}) Leaf = Node Leaf logMsg Leaf
insert
logMsg@(LogMessage _ ts _)
(Node leftTree nodeLogMsg@(LogMessage _ nodeTs _) rightTree)
| ts < nodeTs = Node (insert logMsg leftTree) nodeLogMsg rightTree
| otherwise = Node leftTree nodeLogMsg (insert logMsg rightTree)
To my problem, ghci is warning me on ‘incomplete-patterns’
LogAnalysis.hs:20:1: warning: [-Wincomplete-patterns]
Pattern match(es) are non-exhaustive
In an equation for ‘insert’:
Patterns of type ‘LogMessage’, ‘MessageTree’ not matched:
(LogMessage _ _ _) (Node _ (Unknown _) _)
|
20 | insert (Unknown _) msgTree = msgTree
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...
But from my understanding of my implementation, all LogMessage
s of Unknown
types wouldn’t be inserted in a MessageTree
in the first place. I understand that the compiler might not be able to infer this runtime behavior but is there a way to “tell” the compiler that I don’t have to match that pattern?