Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: read a number (Integer or Floating point)

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?

like image 828
mk12 Avatar asked Mar 04 '12 01:03

mk12


1 Answers

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.

like image 105
C. A. McCann Avatar answered Oct 05 '22 22:10

C. A. McCann