pkruk
pkruk

Reputation: 859

Haskell Tree - Show instance

I am learning haskell and i want to print all nodes in tree (depends on height on tree, where height = 0 => leafs). And i thinking, that I create a good function, but i have a problem with show function.

data Tree a = Empty | Node a (Tree a) (Tree a)  deriving (Show)  

tree4 = Node 1 (Node 2 Empty (Node 0 Empty Empty)) (Node 4 Empty Empty)
tree5 = Empty

heightTree::Tree a -> Integer
heightTree Empty = 0
heightTree (Node x l r) =  1 + max (heightTree l ) (heightTree r)

treeLev::Tree a -> Integer -> [a]
treeLev Empty a = []
treeLev (Node x l r ) a = if a == heightTree l || a == heightTree r  then [x] else treeLev l (a-1) ++ treeLev r (a-1)

and when I'm using

*Main> treeLev tree4

<interactive>:105:1:
    No instance for (Show (Integer -> [Integer]))
      arising from a use of `print'
    Possible fix:
      add an instance declaration for (Show (Integer -> [Integer]))
    In a stmt of an interactive GHCi command: print it
*Main> 

Can anyone explain what I'm doing wrong :)?

Upvotes: 0

Views: 486

Answers (1)

jamshidh
jamshidh

Reputation: 12070

The error message you give isn't telling you that you can't print the tree, it is telling you that you are trying to print out a function, and it can't do that.

You didn't show us the line with the show or print, so we can't see how to fix it, but that is what the message tell me.

Upvotes: 2

Related Questions