Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

haskell check for user input errors

Tags:

haskell

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

like image 301
gruber Avatar asked Dec 29 '22 15:12

gruber


2 Answers

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

like image 116
Yitz Avatar answered Jan 06 '23 19:01

Yitz


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".

like image 26
Paul Johnson Avatar answered Jan 06 '23 19:01

Paul Johnson