[Haskell-beginners] how to do this ?

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Tue Feb 24 16:09:07 UTC 2015


The function

    show :: Show a => a -> String

can be used with

    putStrLn :: String -> IO ()

to get the output on different lines.

The function

    print :: Show a => a -> IO ()
    print = putStr . show
    -- convert to string and then print (without newline)

does not put a newline at the end, to get a newline you use

    putStrLn . show $ ...



On 24 February 2015 at 15:14, Roelof Wobben <r.wobben at home.nl> wrote:

> Yes, that is one of the books I have to read.
> For this exercises chapter 2 and 3 .
>
> Roelof
>
>
> Francesco Ariis schreef op 24-2-2015 om 10:37:
>
>> off list: try to give a more meaningful title to your posts
>> (i.e. "cis194 help exercise 2" instead of "how to do this").
>> It might lead to more answers!
>>
>> Also, have you tried "Learn you a haskell for great good"?
>>
>>
>>
>> On Tue, Feb 24, 2015 at 10:28:28AM +0100, Roelof Wobben wrote:
>>
>>> 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
>>>
>>>
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>>
>>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>



-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150224/2ede4068/attachment-0001.html>


More information about the Beginners mailing list