[Haskell-beginners] how to check the format of a string ?

Roelof Wobben r.wobben at home.nl
Sun Feb 22 20:23:01 UTC 2015


Frerich Raabe schreef op 22-2-2015 om 20:38:
> On 2015-02-22 19:50, Roelof Wobben wrote:
>> Now im facing this problem.
>>
>> There is a log file of strings.
>>
>> Most of them are of this format   char  Number [chars]
>>
>> What is the best way to check if the string has this format ?
>
> For this particular format I'd go for plain pattern matching and 
> guards. Something like
>
>   import Data.Char (isLetter, isDigit)
>
>   isValid :: String -> Bool
>   isValid s = go (words s)
>     where
>       go ([a]:b:_) = isLetter a && all isDigit b
>       go _         = False
>
> This will accept lines starting with a letter followed by some number 
> and then (optionally!) some more text.
>

Thanks,

I can also check if a is equal to I , W or E.

And in the main function check if A =  I/W/E

Roelof



More information about the Beginners mailing list