
Hi, here's another idea : saisie_choix :: IO Int saisie_choix=do putStrLn "Type in 1 digit :" xchoix <- getLine if (length xchoix) /= 1 || head xchoix < '0' || head xchoix > '9' then saisie_choix else do let nchoix=read xchoix::Int return nchoix or, with maybe : {- -------------- MAYBE ------------------ -} -- Fonction principale mainmaybe=do x <- lire if isNothing x then do putStrLn "Donnee invalide" else do putStr " \b" putStrLn ("Resultat : " ++ show (f x)) -- Définition de la fonction de calcul f Nothing = -9999 f (Just x) = 2 * x -- Fonction d'IO qui valide la saisie lire :: IO (Maybe Integer) lire = do -- saisie xn <- getLine let x=read xn ::Integer -- validation if x < 5 then do return (Just x) else do return Nothing Didier. Rahul Kapoor a écrit :
Hi, how can I write an action getInt :: IO Int that works like getLine :: IO String
The most literal way to write getInt would be:
getInt :: IO Int getInt = do s <- getLine return (read s)
which is just a more verbose version of:
getInt' = read `fmap` getLine
The above versions don't do any error checking, so you might prefer a getInt :: IO Maybe Int which returns Nothing in case the input is not an integer.
You should read up on Monadic IO in Haskell. The Real World Haskell book is probably a good starting point.
Rahul _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners