I am really new in Haskell and I am trying to create a Huffman Tree and I can't figure it out how until the end.
My definition for the tree looks like this: data HuffTree = Node Int HuffTree HuffTree | Leaf (Int, Char)
So far, I have a function insTree :: HuffTree -> HuffTree -> HuffTree
that inserts a Node with it's subtrees in a tree and returns the new tree. A function makePair :: HuffTree -> HuffTree -> HuffTree
that takes two trees and makes a new one having as subtrees the original two trees and as value the sum of the values in the first two trees. And a function value :: HuffTree -> Int
that returns the value from each node.
My problem is with the function makeHuffTree :: [(Int, Char)] -> HuffTree
which looks like this:
makeHuffTree :: [(Int, Char)] -> HuffTree
makeHuffTree lst = merge leafList
where
leafList = map (\ ((x,c)) -> Leaf (x,c)) lst
merge [] = []
merge [t] = [t]
merge (t1 : t2 : tree) = insTree (makePair t1 t2) tree
I know it is a problem with this function but I don't know how to deal with it. The error that I get is this:
Couldn't match expected type `[a0]' with actual type `HuffTree'
In the return type of a call of `insTree'
In the expression: insTree (makePair t1 t2) tree
In an equation for `merge':
merge (t1 : t2 : tree) = insTree (makePair t1 t2) tree
Can you give a hint on how to solve this problem?