
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) ? I try: instance Show (Tree k v) where show EmptyTree = show "Empty" show (Node (Int, Int) left right) = .. But get error: Not in scope: data constructor `Int' I have a function: fillTree :: Int -> Tree Int Int -> Tree Int Int fillTree 1000000 tree = tree fillTree x tree = let a = treeInsert (x, x) EmptyTree in fillTree (x + 1) a If i execute it: No instance for (Show (Tree Int Int)) arising from a use of `print' How can i make Show instance for my Tree? Thank you.

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

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.

The documentation for the Show typeclass has this very example: http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.htm... The summary? you need to define either showPrec or show, the latter of which is simpler, it is just a -> String. So: instance Show (Tree Int Int) where show (Tree (Node (k,v)) left right) = ... On Jul 21, 2011, at 10:55 AM, Александр wrote:
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

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....
).
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 Александр
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@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (4)
-
Daniel Patterson
-
Steffen Schuldenzucker
-
Willem Van Lint
-
Александр