我正在尝试为二叉树创建自己的Eq
实例:
data Tree a = Empty | Node a (Tree a) (Tree a)
instance Eq a => Eq (Tree a) where
(Empty == Empty) = True
(Empty == _) = False
(_ == Empty) = False
(Node x xl xr) == (Node y yl yr) = (x == y) && xl == yl && xr == yr
我收到以下错误消息:
Parse error in pattern: Empty == Empty
你能告诉我我错在哪里吗?
答案 0 :(得分:1)
定义运算符或函数时,不得使用括号。请记住
a ???? b
只是
的语法糖(????) a b
因此,您的模式应该是这样的:
Empty == Empty = True
Empty == _ = False
_ == Empty = False
Node x xl xr == Node y yl yr = ...