[Haskell-cafe] Make Show instance

Steffen Schuldenzucker sschuldenzucker at uni-bonn.de
Thu Jul 21 16:52:01 CEST 2011


Hi.

On 07/21/2011 04:45 PM, Александр wrote:
> Hello,
>
> I have binary tree, with every leaf tuple - (k,v):
>
> data Tree k v = EmptyTree
> | Node (k, v) (Tree k v) (Tree k v)
>
> How can i make Show Instance for (Tree Int Int) ?

The easiest way is automatic derivation:

data Tree k v = EmptyTree
   | Node (k, v) (Tree k v) (Tree k v)
   deriving (Eq, Ord, Show, Read)
   -- you normally want at least these four.

Then the compiler automatically declares the instances for you, given 
that 'k' and 'v' have them. For k = v = Int, this is the case.

>
> I try:
>
> instance Show (Tree k v) where
> show EmptyTree = show "Empty"
> show (Node (Int, Int) left right) = ..

I'm afraid to say that, but 'Int' doesn't make sense at this place. You 
would want

 > show (Node (x, y) left right) = ...

instead. (That is, use any variables. Variables have to be lowercase.)

-- Steffen



More information about the Haskell-Cafe mailing list