[Haskell-cafe] Parsec float

Tillmann Vogt Tillmann.Vogt at rwth-aachen.de
Fri May 29 05:02:47 EDT 2009


Bartosz Wójcik wrote:
> Hi Everybody (especially Parsec Creator),
>
> is there any reason why float parses only positive numbers?
>
> I find following defition:
>
> float           = lexeme floating   <?> "float"
>
> floating        = do{ n <- decimal
>                         ; fractExponent n
>                         }
>
> If floating was defined like
>
> floating        = do{ n <- integer ...
>
> or
>
> floating        = do{ n <- int ...
>
> instead  then it would parse negative ones as well.  
>
>   

Hi Bartek,

I had the same problem. Daan Leijen gave me a similar answer than Malcom 
Wallace just gave you:

"Usually the minus sign is treated as an operator in the language and treated as a separate token"

He also gave me a workaround which finally resulted in this:

myfloat = try (do{ symbol "-"; n <- float; return (negate n) }) <|>

          try float <|>

              do { i<-integer; return(fromIntegral i) } -- 0 is not 
recognized as a float, so recognize it as an integer and then convert it 
to float





More information about the Haskell-Cafe mailing list