[Haskell-beginners] Maybe and IO
legajid
legajid at free.fr
Sun Dec 27 16:03:23 EST 2009
Hi,
My purpose is to get data from the keyboard and return a maybe value,
saying "nothing" if the data entered is invalid.
I get a message "could'nt match expected IO Integer against inferred
type maybe integer" on line x<- getdata
Why should x be Maybe ? getdatanum is, but getdata is not.
Is it possible to write only one function rather than 2 distinct ones ?
Thanks for your help,
Didier.
Here's my code
getdata :: IO Integer
getdata=do
x<-getLine
let xn=read x ::Integer
return xn
getdatanum :: Maybe Integer
getdatanum = do
x <- getdata
{-
if x < 5
then do
return (Just x)
else do
return Nothing
-}
return (Just x)
More information about the Beginners
mailing list