haskell check for user input errors
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".
I like to do this with Parsec when it's command line options. How do I do this, given that a command line is a list of strings and not a string? First, you need to create some simple primitives for parsing lists of String
(instead of lists of Char
):
http://github.com/solidsnack/system-uuid/blob/master/Options.hs
Then you can compose those to parse your options, as I've done at the bottom of this file:
http://github.com/solidsnack/system-uuid/blob/master/Main.hs
This is really the most powerful way to do it and it leverages the heavy investment any Haskell programmer makes in parser combinators (just as heavy as any Perl programmer makes in regexen).
I would allow for trailing whitespaces like that:
import Data.Maybe
import Data.Char
maybeRead :: Read a => String -> Maybe a
maybeRead = fmap fst . listToMaybe . filter (null . dropWhile isSpace . snd) . reads
精彩评论