[Haskell] Overloading show

J. Garrett Morris J.Garrett.Morris at Dartmouth.EDU
Fri Sep 10 18:50:45 EDT 2004

--- Stephan Zaubzer wrote:
data Entry a  = EmptyEntry | MakeEntry a a

showEntry :: (Show a) => Entry a -> String
showEntry EmptyEntry = "Empty"
showEntry MakeEntry a b = show a ++ ": " ++ show b
--- end of quote ---

That's a good start.  All you need to do now is make Entry an instance of the Show class:

> instance (Show a) => Show (Entry a)
>    where show = showEntry

Also, you have a small syntax error in showEntry - you probably want:

> showEntry (MakeEntry a b) = show a ++ ": " ++ show b

Finally, using ShowS is generally better for this kind of thing.  <http://www.haskell.org/tutorial/stdclasses.html> describes the disadvantages of using repeated appends and the advantages of ShowS.  Hope that helps!  /gXm

More information about the Haskell mailing list