reads "7a7" :: [(Int, String)] [(7,"a7")] Prelude> reads "7e7" :: [(Int, St...">

Why can't Haskell read "7e7", but can read "7a7"?

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. All of them work, except for 'e' . Haskell seems to be trying to interpret the number in scientific notation, but that is not possible because I ask for Int .

It seems to me that this is a mistake.

+49
string haskell
Mar 27 '14 at 13:28
source share
3 answers

GHC is really 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 token, which means that for "7e7" it gives [("7e7", "")] because "7e7" is an integer token for a floating point literal. Then he tries to get the full syntax from readPos , which in this case is the argument for which Numeric.readDec was passed, and readDec will give, correctly, [(7, "e7")] for the string "7e7". This does not match the pattern matching (n, "") , and ends as [] .

I think it should just be as follows:

 read'' = readPos 
+44
Mar 27 '14 at 1:58
source share

7e7 :: Fractional a => a , so it cannot be read as Int , but it can be read as Float or Double .

 ghci> :t 7e7 7e7 :: Fractional a => a 
+2
Mar 27 '14 at 23:04
source share

What version of GHC are you using?

Here is the edited terminal session output in my setup:

 GHCi, version 7.4.1: http://www.haskell.org/ghc/ :? for help Prelude> reads "7a7" :: [(Int, String)] [(7,"a7")] Prelude> reads "7e7" :: [(Int, String)] [(70000000,"")] Prelude> 

There is ambiguity in how to interpret the input. I usually think that interpreting "7e7" as an Int 70,000,000 would be acceptable. How should the compiler know to break the line after the first digit?

0
Apr 2 '14 at 20:41
source share



All Articles