问题
Consider the following type:
data LTree a = Leaf a | Fork (LTree a) (LTree a)
build :: [(a,Int)] -> LTree a
build l = fst (buildaccum 0 l)e
I have a list and want to build a tree
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
In ghci, I get the following error:
Couldn't match expected type (LTree a, [(a, Int)])' with actual type LTree a'
Can you spot the error, please?
回答1:
buildAccum
must return a pair type (LTree a, [(a, Int)])
, but in your first guarded statement you return a raw LTree a
: Leaf a
.
来源:https://stackoverflow.com/questions/21650802/error-in-list-to-tree