[Haskell-cafe] how can I do this the best

Roelof Wobben r.wobben at home.nl
Tue Feb 24 07:52:24 UTC 2015


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 only thing Im facing now It the Error part.

Messagetype has this signature :

data MessageType = Info
                  | Warning
                  | Error Int
   deriving (Show, Eq)

So Error has a Messagatype which contains 2 things.
but because of the use of words I looks like this I think [ "E", 46, 
23456 , "This", "is", "a", "error". ]
Can I somehow use unwords here to to make the Messagetype work.

Roelof



Richard A. O'Keefe schreef op 24-2-2015 om 2:01:
> On 24/02/2015, at 3:50 am, Roelof Wobben <r.wobben at home.nl> wrote:
>> isValid :: [Char] -> Bool
>> isValid s = go (words s)
>>      where
>>        go ([a]:b:_) = isLetter a && all isDigit b
>>        go _         = False
> My attention was caught by the remarkably unhelpful name "go".
> What goes?  Where does it go?  What does going mean?
>
> This is a good time to use a 'case':
>
> isValid s =
>    case words s of
>      [a]:b:_ -> isLetter a && all isDigit b
>      _       -> False
>
>> parseMessage :: [Char] -> [Char]
>>
>> parseMessage s = isValid s
>>      where
>>          isValid = "Geldige string"
>>          _       = "Ongeldige string"
> In the first line of this function, you are calling
> isValid with an argument.  But you are defining
> isValid to be a string!
>
> parseMessage s =
>    if isValid s then "Geldige string" else "Ongeldige string"
>
> "parseMessage: is a bad name because the purpose of the
> function is *NOT* to break the message into parts and
> deliver those parts but to CHECK or VALIDATE or CLASSIFY
> the message.
>
> The code
>
> import Data.Char
>
> isValid s =
>    case words s of
>      [a]:b:_ -> isLetter a && all isDigit b
>      _       -> False
>
> parseMessage s =
>    if isValid s then "Geldige string" else "Ongeldige string"
>
> main =
>      putStrLn $ parseMessage "I 4764 He trusts to you to set them free,"
>
> compiles without warnings and produces the result I think you want.
>
>
>



More information about the Haskell-Cafe mailing list