Haskell树 - 显示实例

时间:2014-12-07 16:37:11

标签: function haskell functional-programming ghci

我正在学习haskell,我想打印树中的所有节点(取决于树上的高度,其中height = 0 => leafs)。我想,我创造了一个很好的功能,但我有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)

当我使用

*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> 

任何人都可以解释我做错了什么:)?

1 个答案:

答案 0 :(得分:2)

您提供的错误消息并不是告诉您无法打印树,它告诉您正在尝试打印出一个功能,而且它无法执行此操作。

您没有向我们展示showprint的行,因此我们无法看到如何修复它,但这就是消息告诉我的内容。< / p>