
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)

Didier, See below... legajid wrote:
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)
The type of getdatanum should be IO (Maybe Integer). Maybe and IO are both monads, and that's why the error messages can be a bit confusing sometimes. Yes, you certainly can write this as one function. Steve
participants (2)
-
legajid
-
Stephen Blackheath [to Haskell-Beginners]