[Haskell-cafe] Re: Proper round-trip HughesPJ/Parsec for Doubles?

Christian Maeder Christian.Maeder at dfki.de
Wed Feb 24 08:24:07 EST 2010


Andy Gimblett schrieb:
> For the record, here's the final improved version:

Hi Andy,

I hope you don't mind if I make some style comments to your "final" version.

1. break the line after "do"
(to avoid a layout change when change name or arguments of float' or
rename the variable "e")

2. The "t :: TokenParser st" is only used for the white spaces.
This should be done separately (use "lexeme" from the TokenParser if you
really need to). Just using "spaces" is also an alternative.

3. "liftCtoS" is only applied to '-', so an "optSign" would suffice.
  optSign = option "" $ fmap (: []) (char '-')

(read also allows a capital 'E' and a '+' before the exponent, but no
initial '+' sign. The decimal point is optional. Also "NaN" and
"Infinity" can be read, both possibly preceded by a '-' sign followed by
spaces. But you may restrict yourself to the possible outputs of show,
which would include "NaN" and "Infinity", though.)

It may make sense to use something like readMaybe (which is missing in
the Prelude) instead of "read" to allow the parser to fail more nicely.

Btw I observed the following problem with read (that readMaybe would
also not solve).
http://hackage.haskell.org/trac/ghc/ticket/3897

Cheers Christian

> 
> float' :: TokenParser st -> GenParser Char st Double
> float' t = do n <- liftCtoS '-'
>               w <- many1 digit
>               char '.'
>               f <- many1 digit
>               e <- option "" $ do char 'e'
>                                   n' <- liftCtoS '-'
>                                   m <- many1 digit
>                                   return $ concat ["e", n', m]
>               whiteSpace t
>               return $ read $ concat [n, w, ".", f, e]
>   where liftCtoS a = option "" (char a >> return [a])
> 
> Thanks for all the help, again.
> 
> -Andy
> 
> -- 
> Andy Gimblett
> http://gimbo.org.uk/


More information about the Haskell-Cafe mailing list