[Haskell-beginners] how to do this ?

Roelof Wobben r.wobben at home.nl
Tue Feb 24 09:28:28 UTC 2015


Hello,

Im trying to solve exercise 2 of this course : 
http://www.seas.upenn.edu/~cis194/spring13/hw/02-ADTs.pdf

I have this till so far :

-- | Main entry point to the application.
{-# OPTIONS_GHC -Wall #-}

module LogAnalysis where

import           Data.Char (isDigit, isLetter)
import           Log

-- | checks if the String is of the format char int
isValid :: String -> Bool
isValid s =
   case words s of
     [a]:b:_ -> isLetter a && all isDigit b
     _       -> False

-- | Parse the String to make the right logmessage
parse_line :: String -> LogMessage
parse_line s =
    case words s of
         ("I":time:text)           -> LogMessage Info (read time) 
(unwords text)
         ("W":time:text)           -> LogMessage Warning (read time) 
(unwords text)
         ("E":errorcode:time:text) -> LogMessage (Error (read 
errorcode)) (read time) (unwords text)
         _                         ->  Unknown "This is not in the right 
format"


-- | here the actual function which uses isValid and parse to make the 
right log messages
parseMessage :: String -> LogMessage
parseMessage s =
     if  isValid(s) then parse_line(s) else Unknown "This is not the 
right format"


parse :: String -> [logMessage]
parse s =  parse_line s


-- | The main entry point.
main :: IO ()
main = do
     print $ show (parseMessage "I 4681 ehci 0xf43d000:15: regista14: 
[0xbffff 0xfed nosabled 00-02] Zonseres: brips byted nored)")
     print $ show (parseMessage "W 3654 e8] PGTT ASF! 00f00000003.2: 
0x000 - 0000: 00009dbfffec00000: Pround/f1743colled")
     print $ show (parseMessage "E 47 1034 'What a pity it wouldn't 
stay!' sighed the Lory, as soon as it was quite")

But I do not see how I can get the output of the parse_line into a List.

Can anyone give me a tip ?

Roelof





More information about the Beginners mailing list