You may find this helper function useful: > showAp :: Show a => Int -> String -> a -> ShowS > showAp n f x = showParen (n > ap_prec) inside > where > inside = showString f . showString " " . showsPrec (app_prec+1) x > ap_prec = 10 -- "precedence" of function application; higher than any > infix operator.
You can use it like so: > instance Show x => Show (Foo x) where > showsPrec n foo = showAp n "list_foo" (foo_list foo) See the Haskell 98 report section 10.5 "Derived Instances: An Example": http://www.haskell.org/onlinereport/derived.html -- ryan On Tue, Aug 5, 2008 at 8:41 PM, Jonathan Cast <[EMAIL PROTECTED]> wrote: > On Tue, 2008-08-05 at 20:28 +0100, Andrew Coppin wrote: >> Suppose we have the following: >> >> data Foo x >> list_foo :: [x] -> Foo x >> foo_list :: Foo x -> [x] >> >> What would be the best way to write a Show instance? >> >> The thing that I came up with is >> >> instance (Show x) => Show (Foo x) where >> show foo = "list_foo " ++ show (foo_list foo) >> >> But apparently you're supposed to use the strange "showsPrec" method >> that I don't understand. So can somebody show me a correct instance >> definition for this type please? > > 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). > > jcc > > > _______________________________________________ > Haskell-Cafe mailing list > [email protected] > http://www.haskell.org/mailman/listinfo/haskell-cafe > _______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
