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?
Your function will indeed accept Integer
, Float
, or any other Num
instance. However, which type it accepts, and by extension how it parses the String
, is not determined by the input it receives, it's determined by what type the result should be based on what you do with it.
Say you use getNum
and pass the resulting value to something that needs a Float
; in that case, it will parse a Float
value. If you pass it to something that needs an Integer
instead, it will parse that.
As for why it assumes Integer
, there is a "defaulting" system for ambiguous types specified in the Haskell Report, and the rules say that an ambiguous type with a Num
constraint should default to Integer
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With