2014-02-08 43 views
0

考慮以下類型:錯誤列表樹

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 [email protected]((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

buildAccum必須返回對類型(LTree a, [(a, Int)]),但在你的第一個守衛語句返回原始LTree aLeaf a

+0

感謝您指出。我編輯了我的問題。謝謝你的幫助! – user3276667