Proposal: Add Text.Read.maybeRead :: Read a => String -> Maybe a

John Meacham john at repetae.net
Thu Feb 7 22:48:35 EST 2008


On Thu, Feb 07, 2008 at 10:46:48AM -0800, Don Stewart wrote:
> This function is typically defined once per project. So its
> about time this safe variant of 'read' made it into the base.
> 
>     maybeRead :: Read a => String -> Maybe a
>     maybeRead s = case reads s of
>         [(x, "")] -> Just x
>         _         -> Nothing
> 
> Consideration period: 1 week.


May I suggest 'readM'?

It is just the obvious generalization of the 'readIO' that is in the
Prelude, that it isn't generalized is a bug in the prelude IMHO. (and it
is signifigantly more useful)

>     readM :: (Monad m,Read a) => String -> m a
>     readM s = case reads s of
>         [(x, "")] -> return x
>         _         -> fail "readM: no parse"


        John

-- 
John Meacham - ⑆repetae.net⑆john⑈


More information about the Libraries mailing list