Abstract Data Types Exercise
Abstract Data Types Exercise
I 147 mice in the air, Im afraid, but you might catch a bat, and
E 2 148 #56k istereadeat lo d200ff] BOOTMEM
Note that LogMessage has two constructors: one to represent normallyformatted log messages, and one to represent anything else that does
not fit the proper format.
Weve provided you with a module Log.hs containing these data
type declarations, along with some other useful functions. Download
Log.hs and put it in the same folder where you intend to put your
homework assignment. Please name your homework assignment
LogAnalysis.hs (or .lhs if you want to make it a literate Haskell
document). The first few lines of LogAnalysis.hs should look like
this:
{-# OPTIONS_GHC -Wall #-}
module LogAnalysis where
import Log
which sets up your file as a module named LogAnalysis, and imports the module from Log.hs so you can use the types and functions
it provides.
which parses an individual line from the log file. For example,
parseMessage "E 2 562 help help"
== LogMessage (Error 2) 562 "help help"
Once we can parse one log message, we can parse a whole log file.
Define a function
parse :: String -> [LogMessage]
which parses an entire log file at once and returns its contents as a
list of LogMessages.
To test your function, use the testParse function provided in the
Log module, giving it as arguments your parse function, the number
of lines to parse, and the log file to parse from (which should also be
in the same folder as your assignment). For example, after loading
your assignment into GHCi, type something like this at the prompt:
testParse parse 10 "error.log"
Dont reinvent the wheel! (Thats so last week.) Use Prelude functions to make your solution as concise, high-level, and functional as
possible. For example, to convert a String like "562" into an Int, you
can use the read function. Other functions which may (or may not)
be useful to you include lines, words, unwords, take, drop, and (.).
Note that MessageTree is a recursive data type: the Node constructor itself takes two children as arguments, representing the left and
right subtrees, as well as a LogMessage. Here, Leaf represents the
empty tree.
A MessageTree should be sorted by timestamp: that is, the timestamp of a LogMessage in any Node should be greater than all timestamps of any LogMessage in the left subtree, and less than all timestamps of any LogMessage in the right child.
which inserts a new LogMessage into an existing MessageTree, producing a new MessageTree. insert may assume that it is given a
sorted MessageTree, and must produce a new sorted MessageTree
containing the new LogMessage in addition to the contents of the
original MessageTree.
However, note that if insert is given a LogMessage which is
Unknown, it should return the MessageTree unchanged.
[Note: there are much better ways to sort a list; this is just an exercise to get you working with recursive data structures!]
Exercise 5 Now that we can sort the log messages, the only thing
left to do is extract the relevant information. We have decided that
relevant means errors with a severity of at least 50.
Write a function
Miscellaneous
We will test your solution on log files other than the ones we have
given you, so no hardcoding!
You are free (in fact, encouraged) to discuss the assignment with
any of your classmates as long as you type up your own solution.
Epilogue