Numeric literals

Mark Carroll mark@chaos.x-philes.com
Wed, 29 Aug 2001 12:19:11 -0400 (EDT)


On Tue, 28 Aug 2001, Patrik Jansson wrote:
(snip)
> Prelude> reads "123.45a" :: [(Float,String)]
> [(123.45,"a")]
> Prelude> reads "123a.45" :: [(Float,String)]
> [(123.0,"a.45")]
> Prelude> reads "a123.45" :: [(Float,String)]
> []
> Prelude> reads "Infinity" :: [(Float,String)]
> *** Exception: Ratio.%: zero denominator

On 28 Aug 2001 kahl@heraklit.informatik.unibw-muenchen.de wrote:

> You might want to wrap it in something like the following:
>
> read' :: Read a => String -> Maybe a
> read' s = case reads s of
>             ((a,[]):_) -> Just a
>             _          -> Nothing

Thanks very much for this, too. That is also most useful. This is a great
list - I'll try not to abuse everyone's willingness to help! (-: I'm still
trying to understand Chris Angus' suggestion, but it's just a matter of me
spending enough time on it. I assume the Infinity problem above is just a
bug that isn't fixed in that GHC.

-- Mark