<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">Found it <br>
<br>
What do you experts think of this :<br>
<br>
-- | Main entry point to the application.<br>
{-# OPTIONS_GHC -Wall #-}<br>
<br>
module LogAnalysis where<br>
<br>
import Data.Char (isDigit, isLetter)<br>
import Log <br>
<br>
isValid :: String -> Bool<br>
isValid s = <br>
case words s of<br>
[a]:b:_ -> isLetter a && all isDigit b<br>
_ -> False<br>
<br>
parse :: String -> LogMessage<br>
parse s = <br>
case words s of <br>
("I":time:text) -> LogMessage Info (read
time) (unwords text)<br>
("W":time:text) -> LogMessage Warning (read
time) (unwords text)<br>
("E":errorcode:time:text) -> LogMessage (Error (read
errorcode)) (read time) (unwords text)<br>
_ -> Unknown "This is not in
the right format"<br>
<br>
parseMessage :: String -> LogMessage <br>
parseMessage s = <br>
case isValid(s) of <br>
true -> parse(s)<br>
false -> Unknown "This is not the right format"<br>
<br>
<br>
-- | The main entry point.<br>
main :: IO ()<br>
main = do<br>
print $ show (parseMessage "I 4681 ehci 0xf43d000:15:
regista14: [0xbffff 0xfed nosabled 00-02] Zonseres: brips byted
nored)")<br>
print $ show (parseMessage "W 3654 e8] PGTT ASF!
00f00000003.2: 0x000 - 0000: 00009dbfffec00000:
Pround/f1743colled")<br>
print $ show (parseMessage "E 47 1034 'What a pity it wouldn't
stay!' sighed the Lory, as soon as it was quite")<br>
<br>
Rpe;pf<br>
<br>
<br>
<br>
<br>
Sumit Sahrawat, Maths & Computing, IIT (BHU) schreef op
24-2-2015 om 4:26:<br>
</div>
<blockquote
cite="mid:CAJbEW8MJ_8s6z1J8wJTq1etwTKNjCzFsfu7zq15WCmsjaoKvEw@mail.gmail.com"
type="cite">
<div dir="ltr">From the homework:
<div><br>
</div>
<div><font face="monospace, monospace">data MessageType = Info</font></div>
<div><font face="monospace, monospace"> |
Warning</font></div>
<div><font face="monospace, monospace"> | Error
Int</font></div>
<div><font face="monospace, monospace"> deriving (Show, Eq)</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">data LogMessage =
LogMessage MessageType TimeStamp String</font></div>
<div><font face="monospace, monospace"> deriving (Eq, Show)</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">data MaybeLogMessage =
ValidM LogMessage -- A valid msg</font></div>
<div><font face="monospace, monospace"> |
InvalidLM String -- Invalid msg</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
<div><font face="monospace, monospace">parseMessage :: String
-> MaybeLogMessage</font></div>
<div><font face="monospace, monospace">parseMessage = undefined</font></div>
<div><font face="monospace, monospace"><br>
</font></div>
To implement <font face="monospace, monospace">parseMessage</font>,
we consume the string from left to right word-by-word.
<div>If the first word is <font face="monospace, monospace">E</font>,
then we read the second word as in integer indicating severity
and proceed further.</div>
<div>Info and Warning don't require more information, so the
next word will be the timestamp in those cases.</div>
<div>If the pattern fails anywhere, we return the whole string
as an <font face="monospace, monospace">InvalidLM</font>.</div>
<div><br>
</div>
<div>Hope this helps.<br>
<div>
<div><br>
</div>
</div>
</div>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On 24 February 2015 at 06:37, Richard
A. O'Keefe <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:ok@cs.otago.ac.nz" target="_blank">ok@cs.otago.ac.nz</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0
.8ex;border-left:1px #ccc solid;padding-left:1ex"><span
class=""><br>
On 24/02/2015, at 5:19 am, Roelof Wobben <<a
moz-do-not-send="true" href="mailto:r.wobben@home.nl">r.wobben@home.nl</a>>
wrote:<br>
<br>
> I tried it another way more like explained on this
page : <a moz-do-not-send="true"
href="http://www.seas.upenn.edu/%7Ecis194/spring13/lectures/02-ADTs.html"
target="_blank">http://www.seas.upenn.edu/~cis194/spring13/lectures/02-ADTs.html</a><br>
><br>
> so I tried this :<br>
><br>
> parseMessage :: [Char] -> [Char]<br>
> parseMessage s<br>
> case Errornumber of<br>
> IsDigit Errornumber -> "Geldige string"<br>
> otherwise -> "Ongeldige string"<br>
> where<br>
> Error = s words<br>
> Errornumber = Error(ErrorNumber _ _ )<br>
> Errorcode = Error(_ Errorcode _ )<br>
><br>
> but now I cannot use where :(<br>
<br>
</span>That's not your problem.<br>
<br>
IsDigit ErrorNumber is not a pattern.<br>
<br>
parseMessage s =<br>
if isDigit errorNumber then "Geldige string"<br>
else "Ongelidige string"<br>
where<br>
errorNumber = ???<br>
<br>
is OK.<br>
<br>
Now I cannot make sense of<br>
Error = s words<br>
<br>
identifiers beginning with capital letters are used
for<br>
- module names<br>
- type constructors<br>
- data constructors<br>
You want a variable here, so it must begin with a<br>
lower case letter.<br>
<br>
s words treats a string s as a function and applies
it<br>
to the function words as argument: s(words). But
that<br>
does not type check. You mean words s.<br>
<br>
The result of words s, whatever else it may be, is
not<br>
an error.<br>
<br>
Errornumber = Error(ErrorNumber _ _)<br>
<br>
In the form "expr where pattern = expr", the thing
after<br>
the equal sign must be an expression. But<br>
Error(ErrorNumber _ _) is not an expression. "_" is
a<br>
PATTERN (= I do not care what goes here) but never
an<br>
EXPRESSION (because what value would it have?).<br>
<div class="HOEnZb">
<div class="h5"><br>
<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a moz-do-not-send="true"
href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a moz-do-not-send="true"
href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe"
target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br>
</div>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<div><br>
</div>
-- <br>
<div class="gmail_signature">
<div dir="ltr">
<div>
<div dir="ltr">
<div dir="ltr">
<div>Regards</div>
<div dir="ltr">
<div><br>
</div>
<div>Sumit Sahrawat</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</blockquote>
<br>
</body>
</html>