Basically, I created a data type of a polymorphic tree, and I need a way to count the number of elements in a given tree. Here is the declaration for my Tree data type:
data Tree a = Empty | Leaf a | Node (Tree a) a (Tree a) deriving (Eq, Ord, Show)
So, I can define the Ints tree as follows:
t :: Tree Int t = Node (Leaf 5) 7 (Node (Leaf 2) 3 (Leaf 7))
However, I need a function to count the number of elements in one of these lists. I defined this recursive function, but I get the error: "the intended type is not general enough":
size :: Tree a -> Int size Empty = 0 size (Leaf n) = 1 size (Node xyz) = size x + size y + size z
Is there something here that I should not do?
functional-programming count recursion haskell
benwad
source share