我对Haskell非常陌生,我试图围绕语法(以及习惯声明性语言)进行思考。我创建了一个树型数据类型,我希望能够使用==运算符来比较它们。下面是我有:Haskell中的树相等运算符
data Tree =
Leaf
| Twig
| Branch Tree Tree Tree
deriving Show;
instance Eq Tree where
Leaf == Leaf = True;
(Branch a b c) == (Branch a1 b1 c1) = a==a1 && b==b1 && c==c1;
这似乎输入时的工作:Leaf == Leaf
或Branch Leaf Leaf Leaf == Branch Leaf Leaf Leaf
,但它不断给我一个错误,当我添加Twig == Twig = True;
。此外,没有办法比较Leaf == Branch Leaf Leaf Leaf
。我尝试使用_==_ = False;
但也给了我一个错误。我迷路了,任何帮助将不胜感激!
编辑: 仍然得到错误,具体如下:
[1 of 1] Compiling Main (Tree.hs, interpreted)
Tree.hs:15:5: parse error on input ‘_’
Failed, modules loaded: none.
Prelude> :r
[1 of 1] Compiling Main (Tree.hs, interpreted)
Tree.hs:15:3: parse error on input ‘Twig’
Failed, modules loaded: none.
第一是我带出来的问题枝条==后,留下_ == _。二是无论是在离开。
如果您发布了触发错误的确切代码,那么您可能会解释发生了什么问题。 – 2014-11-05 01:37:09