[Haskell-cafe] how can I do this the best
Sumit Sahrawat, Maths & Computing, IIT (BHU)
sumit.sahrawat.apm13 at iitbhu.ac.in
Tue Feb 24 08:14:25 UTC 2015
On 24 February 2015 at 13:22, Roelof Wobben <r.wobben at home.nl> wrote:
> Thanks,
>
> I rewrote it like this :
>
> parseMessage :: String -> LogMessage
> parseMessage 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"
>
The code looks just fine. Great work.
The trouble is with the third case. The error message is fairly instructive.
log.hs:19:36:
Couldn't match expected type ‘String -> LogMessage’
with actual type ‘LogMessage’
The function ‘LogMessage’ is applied to four arguments,
but its type ‘MessageType -> TimeStamp -> String -> LogMessage’
has only three
In the expression:
LogMessage Error (read errorcode) (read time) (unwords text)
In a case alternative:
("E" : errorcode : time : text)
-> LogMessage Error (read errorcode) (read time) (unwords text)
log.hs:19:47:
Couldn't match expected type ‘MessageType’
with actual type ‘Int -> MessageType’
Probable cause: ‘Error’ is applied to too few arguments
In the first argument of ‘LogMessage’, namely ‘Error’
In the expression:
LogMessage Error (read errorcode) (read time) (unwords text)
Long story short, you meant to write
LogMessage (Error (read errorcode)) ...
instead of
LogMessage Error (read errorcode) ... -- Too many arguments to
LogMessage (error message 1)
-- Too few arguments to
Error (error message 2)
which is a very common error.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150224/d9f1f4fb/attachment.html>
More information about the Haskell-Cafe
mailing list