
Hi i meet a problem when i was using readLn to read a int from user my function is like that do (some thing else that is working) n <- readLn (some thing else) f n x1 x2 x3 …… f has the type of Int -> something else because the type in this function is fixed, if the type of n is not Int, it will have exception However i want to detect whether the user input is valid or not and i have this function import Data.Typeable isInteger :: Typeable a => a -> Bool isInteger n = (typeOf n) == (typeOf (1::Int)) and add to do (some thing else that is working) n <- readLn if isInteger n then (some thing else) f n x1 x2 x3 …… else (something else) but it doesn’t work because of the reason “type has been inferred and fixed” How could i solve this problem? Zongzhe Yuan This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation.

On 2015-02-13 13:00, Zongzhe Yuan wrote:
i meet a problem when i was using readLn to read a int from user
my function is like that
do (some thing else that is working) n <- readLn (some thing else) f n x1 x2 x3 ……
f has the type of Int -> something else because the type in this function is fixed, if the type of n is not Int, it will have exception However i want to detect whether the user input is valid or not
[..]
How could i solve this problem?
readLn is a bit harsh when it comes to error reporting. You could use getLine to read a string and then use readMaybe (in Text.Read) to see whether it's a valid integer. Like: main = do s <- getLine case readMaybe s :: Maybe Int of Just i -> f i Nothing -> putStrLn "Ouch!" -- Frerich Raabe - raabe@froglogic.com www.froglogic.com - Multi-Platform GUI Testing
participants (2)
-
Frerich Raabe
-
Zongzhe Yuan