[Haskell] Beginner - Binary Search Tree Question

htc2011 jakobusbenne at gmail.com
Sat Feb 12 11:39:43 CET 2011


Hi All,

I am learning Haskell and can't understand the following problem. Maybe
somebody could advise me on a solution?

Using GHCI, I have the following definition of a BST:
data Ord a => BST a = EmptyBST | Node ( BST a ) a ( BST a ) deriving (Show)

I want to determine the number of leaves that a tree using the above
definition has:
numLeaves :: Ord a => BST a -> Int
numLeaves EmptyBST = 0
numLeaves (Node b a c)
	| b == EmptyBST = 1 + numLeaves c 
	| c == EmptyBST = 1 + numLeaves b 
	| otherwise = numLeaves b + numLeaves c

However whenever I load my haskell file containing the above code into GHCI,
I get the following error:

Could not deduce (Eq (BST a)) from the context (Ord a)
      arising from a use of `==' at a8.hs:17:3-15
    Possible fix:
      add (Eq (BST a)) to the context of
        the type signature for `numLeaves'
      or add an instance declaration for (Eq (BST a))
    In the expression: b == EmptyBST
    In a stmt of a pattern guard for
                 the definition of `numLeaves':
          b == EmptyBST
    In the definition of `numLeaves':
        numLeaves (Node b a c)
                    | b == EmptyBST = 1 + numLeaves c
                    | c == EmptyBST = 1 + numLeaves b
                    | otherwise = numLeaves b + numLeaves c

Could anybody explain to me what this means? / How to get around this?

Thank you for your time!

-- 
View this message in context: http://haskell.1045720.n5.nabble.com/Beginner-Binary-Search-Tree-Question-tp3382548p3382548.html
Sent from the Haskell - Haskell mailing list archive at Nabble.com.



More information about the Haskell mailing list