[Haskell-cafe] Make Show instance

Willem Van Lint willem at willem.vanlint.name
Thu Jul 21 17:38:48 CEST 2011


Hi,

I think your main problem here is that you use Int in the pattern matching.
The point is that you are matching your value of type Tree k v to a pattern
such as:
- EmptyTree
- (Node (a, b) left right)

Patterns don't contain type information such as Int, but things like value
constructors and variables.
Note the difference between type constructors and value constructors (
http://book.realworldhaskell.org/read/defining-types-streamlining-functions.html
).
So you probably should use something like this:

data Tree k v = EmptyTree
                | Node (k, v) (Tree k v) (Tree k v)

instance (Show k, Show v) => Show (Tree k v) where
     show EmptyTree =
         "Empty"
     show (Node (a, b) left right)  =
        show left ++ "(" ++ show a ++ "," ++ show b ++ ")" ++ show right

I probably use (++) too much here though.
'(Show k, Show v) =>' tells us that the types k and v are of that typeclass
so that we can use show on values of this type.

I'm a beginner too though so I hope I was clear.



2011/7/21 Александр <kommunist1917 at mail.ru>

> Hello,  thank you for reply. I know that i can derive this. But i want to
> know how can i make it by hand.
>
> Thank you.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20110721/bd77f3dd/attachment.htm>


More information about the Haskell-Cafe mailing list