Haskell:镜像二叉树错误:“0”;不能';t匹配预期类型“;

Haskell:镜像二叉树错误:“0”;不能';t匹配预期类型“;,haskell,binary-tree,Haskell,Binary Tree,所以我试图设计一个函数来返回一个二叉树,它是最初发送的二叉树的镜像。我想我已经差不多完成了,但我得到了一个奇怪的“无法匹配预期类型。以下是我的代码: mirror :: BinTree a -> BinTree a mirror (Node x tL tR) = Node x (mirror tR) (mirror tL) 下面是错误: hw1.hs:84:30: Couldn't match expected type `a' with actual type `Bin

所以我试图设计一个函数来返回一个二叉树,它是最初发送的二叉树的镜像。我想我已经差不多完成了,但我得到了一个奇怪的“无法匹配预期类型。以下是我的代码:

mirror :: BinTree a -> BinTree a    
mirror (Node x tL tR) = Node x (mirror tR) (mirror tL)
下面是错误:

hw1.hs:84:30:
    Couldn't match expected type `a' with actual type `BinTree a'
      `a' is a rigid type variable bound by
          the type signature for mirror :: BinTree a -> BinTree a
          at hw1.hs:83:11
    In the first argument of `Node', namely `x'
    In the expression: Node x (mirror tR) (mirror tL)
    In an equation for `mirror':
        mirror (Node tL x tR) = Node x (mirror tR) (mirror tL)

hw1.hs:84:33:
    Couldn't match expected type `a' with actual type `BinTree a'
      `a' is a rigid type variable bound by
          the type signature for mirror :: BinTree a -> BinTree a
          at hw1.hs:83:11
    In the return type of a call of `mirror'
    In the second argument of `Node', namely `(mirror tR)'
    In the expression: Node x (mirror tR) (mirror tL)
Failed, modules loaded: none.
这是我的错。我对树的定义不同。

data BinTree a = Empty | Node (BinTree a) a (BinTree a) deriving (Eq,Show)
*新功能应该是:*


在@Davorak的建议下,我把我的评论变成了一个答案,尽管它并没有真正回答问题,只是说明了问题


使用定义时

data BinTree a=Empty |节点a(BinTree a)(BinTree a)派生(Eq)
OP的原始代码可以编译,但没有编译,因为他使用的定义是

data BinTree a=Empty |节点(BinTree a)a(BinTree a)派生(Eq)

通过修改函数
mirror
,使其与
bintreea
的正确定义模式匹配,问题就解决了。

在@Davorak的建议下,我将我的评论变成了答案,尽管它并没有真正回答问题,只是说明了问题


使用定义时

data BinTree a=Empty |节点a(BinTree a)(BinTree a)派生(Eq)
OP的原始代码可以编译,但没有编译,因为他使用的定义是

data BinTree a=Empty |节点(BinTree a)a(BinTree a)派生(Eq)

通过修改函数
mirror
,使之与
BinTree a
的正确定义模式匹配,问题就解决了。

你能发布你对
BinTree a
的定义吗?你的代码为我编译。啊,先生。谢谢!!我对haskell太陌生了,以至于忘了检查我的二叉树的定义…它是节点TLXtR和not Node x tL tR…谢谢!很好!现在如果你
镜像为空
,会发生什么?老实说,不确定会发生什么。我实际上做了以下操作:镜像为空=为空,因为我必须返回一个BinTree?@bheklillr你应该将你的评论转化为答案,因为它解决了问题,你将获得解决问题的信用,并且它将被删除为Haskell标记的问题提供答案统计信息,以便更好地回答问题。你能发布你对
BinTree a
的定义吗?你的代码为我编译。啊,先生。谢谢!!我对Haskell太陌生了,以至于我忘了检查我的二叉树的定义…它是Node tL x tR而不是Node x tL tR…谢谢!很好!如果你
 mirror Empty
?老实说,我不确定会发生什么。我确实做了以下操作:mirror Empty=Empty,因为我必须返回BinTree?@bheklir您应该将您的评论转换为答案,因为它解决了问题,您将获得解决问题的积分,并且它将使Haskell的答案统计信息变得更好,因此问题也会更好。
mirror :: BinTree a -> BinTree a
mirror Empty = Empty
mirror (Node tL x tR) = Node (mirror tR) x (mirror tL)