> > I thought Haskell 1.3 allowed "2" as a valid Float for read.
> > What implementation are you using?
>
> Our hbc compiler will indeed provide a `read' function that parses strings like
> "2" as Floats but our Hugs (1.3) and ghc (2.01) won't. In any case, input like
> "foo" will still cause the program to blow up at run-time. I ended up
> supplying the following definition:
>
> > atof:: String -> Maybe Float
> > atof str =
> > case span isDigit str of
> > (_,"") -> Just (read (str++".0"))
> > (_,'.':f_str) | all isDigit f_str -> Just (read str)
So here's what I'd do (having a working read):
> atof :: String -> Maybe Float
> atof str =
> case reads str of
> [(f, "")] -> Just f
> _ -> Nothing
That way you don't have to worry about the exact lexical syntax
of floats (e.g. exponents) in your code.
-- Lennart