
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

On Fri, May 29, 2009 at 4:02 AM, Tillmann Vogt
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
participants (2)
-
Derek Elkins
-
Tillmann Vogt