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

Richard A. O'Keefe ok at cs.otago.ac.nz
Tue Feb 24 01:01:26 UTC 2015


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