[Haskell-cafe] Parsec float

Derek Elkins derek.a.elkins at gmail.com
Fri May 29 19:34:50 EDT 2009


On Fri, May 29, 2009 at 4:02 AM, Tillmann Vogt
<Tillmann.Vogt at rwth-aachen.de> wrote:
> 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"

There's a more pointed reason related to the ones given.  If the float
parser parses signed floats, what then do you do when you want to
parse unsigned floats?  It's trivial to go the one way, it's less
trivial to go the other way.

Incidentally, I'd probably write something like
((try $ negate <$ char '-') <|> pure id) <*> float -- untested


More information about the Haskell-Cafe mailing list