I am using this implementation of maybeRead:
maybeRead :: (Read a) => String -> Maybe a
maybeRead = fmap fst . listToMaybe . filter (null . dropWhile isSpace . snd) . reads
and my own getNum function which prompts until it gets valid input:
getNum :: (Num a, Read a) => String -> IO a
getNum s = do
putStr s
input <- fmap maybeRead getLine
if isNothing input
then getNum s
else return $ fromJust input
However if I enter 5.2 it treats it as bad input—Why? There is zero occurences of Int and Integer in my code. I am only using Num, since I want to accept any kind of number.
If I call it explicitly as getNum "Enter a number: " :: IO Double, then it works. Do I have to do this? Is Haskell's type system just tricking me into thinking I should be able to do this when really it is impossible without full dynamic typing? If so then why does my code even compile; why does it assume integers?
isNothingandfromJust, so theifexpression can be replaced withcase input of Nothing -> getNum s; Just x -> return x.