[Haskell-cafe] How to define Show [MyType] ?

Daniel Fischer daniel.is.fischer at web.de
Fri Dec 5 18:32:21 EST 2008


Am Samstag, 6. Dezember 2008 00:13 schrieb Dmitri O.Kondratiev:
>
> Thanks everybody for your help!
> I tried to implement showList, but get the same error:
>
> {--
> -- from Prelude:
> type *ShowS* =
> String<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASt
>ring>->
> String<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASt
>ring> *showList* :: [a] ->
> ShowS<file:///C:/usr/ghc-6.6.1/doc/html/libraries/base/Prelude.html#t%3ASho
>wS> --}
>
> myShows::ShowS
> myShows s = s ++ "\n"
>
> data ShipInfo = Ship {
>       name :: String,
>       kind :: String,
>       canons :: Int
> } deriving Show

No, if you derive the Show instance for ShipInfo, it automatically implements 
the standard showList, too. You have to do it yourself:

data ShipInfo = Ship {
	name :: String,
	kind :: String,
	canons :: Int
      }

instance Show ShipInfo where
	showsPrec p (Ship name kind canons) = ...
	showList xs = showString (unlines $ map show xs)

or whatever you want for showList.
However, somebody said it before, the Show instance should not be used for 
pretty-printing.


>
> -- I get this error again:
> instance (Show [ShipInfo]) where
>     showList [] = myShows []
>     showList (x:xs)  = myShows "x" ++ showList xs
>
>     Illegal instance declaration for `Show [ShipInfo]'
>         (The instance type must be of form (T a b c)
>          where T is not a synonym, and a,b,c are distinct type variables)
>     In the instance declaration for `Show [ShipInfo]'
> Failed, modules loaded: none.
>
> -- What am I doing wrong?
> Thanks!



More information about the Haskell-Cafe mailing list