Error in list to tree

萝らか妹 提交于 2020-03-05 05:37:45

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!