Im writing app in haskell and I would like to know that is the best way to check if users input is ok for example is it int when Im asking for int or if its date when asking for well formed date ?
Thanks for help
Use maybeRead
. That function is a candidate for
inclusion in the Haskell libraries, but it hasn't made it in
yet. So include the code for it. Here's a complete solution:
import Data.Time
import Data.Maybe (listToMaybe)
import System.IO (hSetBuffering, BufferMode(NoBuffering), stdout)
main = do hSetBuffering stdout NoBuffering putStr "Enter an Int: " maybeInt <- fmap maybeRead getLine :: IO (Maybe Int) maybe (putStrLn "That's not an Int!") (putStrLn . ("The Int is " ++) . show) maybeInt putStr "Enter a date: " maybeDate <- fmap maybeRead getLine :: IO (Maybe Day) maybe (putStrLn "That's not a date!") (putStrLn . ("The date is " ++) . show) maybeDate
maybeRead :: Read a => String -> Maybe a maybeRead = fmap fst . listToMaybe . filter (null . snd) . reads
For an Integer the simplest way is to use "reads". This has the type:
type ReadS a = String -> [(a, String)]
reads :: (Read a) => ReadS a
The idea is that for any type which is an instance of the Read class you pass it the string and it tries to parse it as that type. For instance Integer is an instance of Read, so you can think of the type as
reads :: String -> [(Integer, String)]
If it succeeds then the result will contain one entry with the number and the rest of the string. So for instance
reads "45xyz" = [(45, "xyz")]
So in your case just pattern match on [(v, "")] to get the value, and then have acatch-all pattern match complaining that it didn't get an integer.
Doing dates is more complex. Define your format, and then break it up into bits you can recognise using "reads".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With