我正在學習haskell,我想打印樹中的所有節點(取決於樹上的高度,其中height = 0 => leafs)。我想,我創造了一個很好的功能,但是我有一個show功能的問題。Haskell Tree - 顯示實例
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>
任何人都可以解釋我做錯了:)?
'treeLev'接收兩個參數。所以'treeLev tree4'是一個函數。您無法打印功能。 – user3237465 2014-12-07 17:11:43