Try to do:
Prelude> reads "7a7" :: [(Int, String)]
[(7,"a7")]
Prelude> reads "7e7" :: [(Int, String)]
[]
I tested this for all possible characters in the middle. They all work except for 'e'
. It seems as if Haskell tries to interpret the number in scientific notation, but it can't because I'm asking for Int
.
It seems like a bug to me.
GHC is indeed buggy. Its implementation of Numeric.readSigned
uses the following:
read'' r = do
(str,s) <- lex r
(n,"") <- readPos str
return (n,s)
The lex
call will try to parse any lexeme, and this means that for "7e7" it yields [("7e7", "")]
, because "7e7" is a whole lexeme for a floating point literal. Then it tries to get a complete parse out of readPos
, which in this case is an argument for which Numeric.readDec
was passed in, and readDec
will yield, correctly, [(7, "e7")]
for the string "7e7". That fails pattern matching against (n, "")
, and ends up as []
.
I think it should be simply as follows:
read'' = readPos
7e7 :: Fractional a => a
so it can't be read as an Int
, but it can be read as a Float
or Double
.
ghci> :t 7e7
7e7 :: Fractional a => a
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