我对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;
Run Code Online (Sandbox Code Playgroud)
这似乎在输入时起作用: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 ) …Run Code Online (Sandbox Code Playgroud)