Haskell方程二叉树(镜像)

时间:2018-04-14 13:40:43

标签: haskell

您好,我有一些关于Haskell的功课。

The definition of a binary tree has the form:

data Tree a = Leaf a
             | Node a (Tree a) (Tree a)
             | null
Enter the implementation of the equal function checking whether the two binary trees are identical.

Tip
equal :: Eq a => Tree a -> Tree a -> Bool
equal Null Null = {- ... -}
equal {- ... -} = a == b
equal (Node a1 left1 right1) {- ... -} = a1 == a2 && {- ... -}
equal _ _ = False
In the above outline of implementation, replace comments {- ... -} into the appropriate code.

这是我的作业,我找到了一些东西,但我找不到一件事。

equal :: Eq a => Tree a -> Tree a -> Bool
equal Null Null = True
equal a (Leaf b) = a == b
equal (Node a1 left1 right1) (Node a2 left2 right2) = a1 == a2 && equal left1 right2 && equal right1 left2
equal _ _ = False

这是我的代码,equal a (Leaf b) = a == b此部分无效。我没有找到我应该放在那里的东西。

1 个答案:

答案 0 :(得分:1)

该行:

equal a (Leaf b) = a == b

会导致类型不匹配,应该替换为:

equal (Leaf a) (Leaf b) = a == b