我對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