title: Huffman Tree in Haskell
body: <p>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.</p>
  
  <p>My definition for the tree looks like this: <code>data HuffTree = Node Int HuffTree HuffTree | Leaf (Int, Char)</code></p>
  
  <p>So far, I have a function <code>insTree :: HuffTree -&gt; HuffTree -&gt; HuffTree</code> that inserts a Node with it's subtrees in a tree and returns the new tree. A function <code>makePair :: HuffTree -&gt; HuffTree -&gt; HuffTree</code> 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 <code>value :: HuffTree -&gt; Int</code> that returns the value from each node. </p>
  
  <p>My problem is with the function <code>makeHuffTree :: [(Int, Char)] -&gt; HuffTree</code> which looks like this:</p>
  
  <pre><code>makeHuffTree :: [(Int, Char)] -&gt; HuffTree
  makeHuffTree lst = merge leafList
      where
          leafList = map (\ ((x,c)) -&gt; Leaf (x,c)) lst
          merge [] = []
          merge [t] = [t]
          merge (t1 : t2 : tree) = insTree (makePair t1 t2) tree
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Can you give a hint on how to solve this problem?</p>