[Haskell-beginners] Writing an instance of Read?

Daniel Fischer daniel.is.fischer at googlemail.com
Sun Oct 16 02:42:30 CEST 2011


On Sunday 16 October 2011, 02:15:54, Mike Meyer wrote:
> Is there a tutorial - or anything at all, for that matter - on writing
> Read instances? Particularly so that it integrates properly types that
> incoporate your type and derive Read? Real World Haskell gives one
> simple example (which doesn't use the first argument), points out one
> problem with the example, then says "many people find it easier to use
> Parsec." And that's the best such I could find.
> 
>      Thanks,
>      <mike

If you're using GHC, you can directly use Text.ParserCombinators.ReadPrec 
and Text.ParserCombinators.ReadP to write your Read instances (define 
readPrec and optionally readListPrec instead of readsPrec and readList).
If your parser is correct, it will automatically integrate properly with 
derived Read instances.

ReadP has many combinators in common with Parsec, so going from Parsec to 
ReadP is fairly straightforward.



More information about the Beginners mailing list