在树中查找某个元素的路径长度

时间:2017-10-29 11:12:08

标签: haskell

我试过这个:

tWLength  (Node v l r) u            | v == u = 0
                                    | v <  u = 1 + (tWLength l)
                                    | v >  u = 1 + (tWLength r)

然而它返回(在WinGHCi中):

experiments\treetest.hs:170:1: error:
    • Occurs check: cannot construct the infinite type: a1 ~ a -> a1
      Expected type: Tree a -> a1
        Actual type: Tree a -> a -> a1
    • Relevant bindings include
        tWLength :: Tree a -> a1 (bound at experiments\treetest.hs:170:1)
    |
170 | tWLength  (Node v l r) u            | v == u = 0
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

树定义为:

data Tree a = NullTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq, Ord)

1 个答案:

答案 0 :(得分:2)

我现在找到了原因。我在

中只用一个参数调用tWLength
tWLength  (Node v l r) u        | v == u = 0
                                | v <  u = 1 + (tWLength l)
                                | v >  u = 1 + (tWLength r)

应该是什么时候

tWLength  (Node v l r) u        | v == u = 0
                                | v <  u = 1 + (tWLength l u)
                                | v >  u = 1 + (tWLength r u)