[Haskell-cafe] handling read exceptions

Andre Pang ozone at algorithm.com.au
Tue Apr 13 13:12:31 EDT 2004

On 13/04/2004, at 2:17 AM, S. Alexander Jacobson wrote:

> I want to read strings that look like "2"  or
> "hello" into values of type Integer or String.
> The problem is that read requires that strings be
> read as "\"hello\"".  Is there a way either to
> convince read to not require wrapping quotation
> marks or, alternetively, to catch a read
> exception, and do something sane?

Untested code (I'll leave you to come up with a better name than MyRead 

     class MyRead a where myRead :: String -> a
     instance MyRead Integer where myRead = read
     instance MyRead String where myRead = id

% Andre Pang : trust.in.love.to.save

More information about the Haskell-Cafe mailing list