列表到树的错误

时间:2014-02-08 19:30:05

标签: list haskell tree

考虑以下类型:

data LTree a = Leaf a | Fork (LTree a) (LTree a)
build :: [(a,Int)] -> LTree a
build l = fst (buildaccum 0 l)e

我有一个列表,想要构建一个树

buildaccum :: Int -> [(a,Int)] -> (LTree a, [(a,Int)])
buildaccum n l@((a,b):t) |n==b = (Leaf a,t)
                         |n<b = (Fork e d, l2)
     where (e,l1) = buildaccum (n+1) l
           (d,l2) = buildaccum (n+2) l1

在ghci中,我收到以下错误:

Couldn't match expected type (LTree a, [(a, Int)])' with actual type LTree a' 

你能发现错误吗?

1 个答案:

答案 0 :(得分:1)

buildAccum必须返回一对(LTree a, [(a, Int)]),但在您的第一个保护声明中,您会返回原始LTree aLeaf a