I'm trying to do my homework but I get a syntax error that I don't understand.
data CTree a b =
CNode (CTree a b) b (CTree a b) | -- left, value, right
CLeaf a -- value
deriving (Show, Ord)
-- a)
--
instance (Ord a, Ord b) => Ord (CTree a b) where
--
-- Compares two CNode objects
--
(CNode left1 v1 right1) `compare` (CNode left2 v2 right2) =
-- If the value of the first node is greater than
-- the value of the second node, the result is GT.
if (v1 > v2) then GT
-- If the value of the first node is smaller than
-- the value of the second node, the result is LT
else if (v2 < v1) then LT
-- We must compare the child nodes if the values of
-- boths nodes equal.
else if (v1 == v2) then
if (left1 == left2) then
(right1 `compare` right2)
else
(left1 `compare` left2)
main = do
print "foo"
The error is (at the line of main = do)
parse error (possibly incorrect indentation or mismatched brackets)
I am aware that there are some overloads of the compare function missing, but the Syntax Error should not originate from that fact.
compare (CNode left1 v1 right1) (CNode left2 v2 right2) = compare (v1, left1, right1) (v2, left2, right2)v1andv2all three ways it would probably be clearer (and more efficient) to usecase v1 `compare` v2 of ...instead of nestedifs.