[Haskell-cafe] Show me

Jonathan Cast jonathanccast at fastmail.fm
Fri Aug 8 13:59:04 EDT 2008


On Wed, 2008-08-06 at 19:06 +0100, Andrew Coppin wrote:
> Jonathan Cast wrote:
> > instance Show x => Show (Foo x) where
> >   showsPrec n foo = ("list_foo "++) . shows (foo_list foo)
> >
> > You use the n parameter if you've got an infix operator in your syntax
> > and you want to put parentheses around an instance of it if n has the
> > wrong value (either too high or too low, I can never remember).

> OK. So... should the call to shows actually be showsPrec? Or does it not 
> matter in this case?

According to
http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/GHC-Show.html#shows,

   shows = showsPrec 0

(which is a global function like show, not a class method).  In this
case, showsPrec will then further dispatch to your showList method,
which has the default implementation, which will ignore the precedence
parameter.  So it doesn't matter in this case.

> Actually, now that I think of it, my *next* problem is going to be 
> writing a matching Read instance...

Are you familiar with Text.ParserCombinators.ReadP? 
http://www.haskell.org/ghc/docs/latest/html/libraries/base/Text-ParserCombinators-ReadP.html?

It's an explicit monad, so it's rather easier to use that ReadS.

jcc




More information about the Haskell-Cafe mailing list