title: Find Sum of leaves
body: <p>I am supposed to write a code such that</p>
  
  <blockquote>
    <p>A polymorphic tree type with nodes of an arbitrary number of children might be represented as follows (note that the leaves store a list, and interior nodes store list of “ListTree”s):</p>
  </blockquote>
  
  <pre><code>data ListTree a = ListLEAF [a] | ListNODE [(ListTree a)]
   deriving (Show, Read, Eq)
  </code></pre>
  
  <blockquote>
    <p>Write a function <code>foldListTree</code> that takes a function (<code>f</code>), a base value (<code>base</code>), and a ListTree (<code>t</code>) and combines the values in the lists of the leaf notes in tree <code>t</code> by applying function <code>f</code>. (The leaves of the tree are scanned from left to right). <code>foldListTree</code> is invoked as: </p>
    
    <p><code>foldListTree f base t</code> where <code>f</code> is the combining function of type <code>a-&gt;a-&gt;a</code>. The type of <code>foldListTree</code> should be:</p>
  </blockquote>
  
  <pre><code>foldListTree :: (a -&gt; a -&gt; a) -&gt; a -&gt; ListTree a -&gt; a
  </code></pre>
  
  <p>I am trying to read every node in the list but I guess it's getting into an infinite loop.</p>
  
  <pre class="lang-hs prettyprint-override"><code>data ListTree a = ListLEAF [a] | ListNODE [(ListTree a)] deriving (Show, Read, Eq)
  
  
  foldListTree :: (Num a) =&gt; (a -&gt; a -&gt; a) -&gt; a -&gt; ListTree a -&gt; a 
  foldListTree op base (ListLEAF []) = base
  foldListTree op base (ListNODE []) = base
  foldListTree op base (ListLEAF [a]) = foldr op base [a]
  foldListTree op base (ListNODE b) = (op (foldListTree op base x) 
                                          (foldListTree op base (ListNODE xs)))
                                          where x:xs = b
  
  t4 = ListNODE
         [ ListNODE
             [ ListLEAF [1,2,3]
             , ListLEAF [4,5]
             , ListNODE [ListLEAF [6], ListLEAF []]
             ]
         , ListNODE []
         , ListLEAF [7,8]
         , ListNODE [ListLEAF [], ListLEAF []]
         ]
  </code></pre>
  
  <p>Command: <code>foldListTree (+) 0 t4</code></p>
  
  <blockquote>
    <p>> Error : *** Exception: indi.hs:(86,1)-(90,54): Non-exhaustive patterns in function <code>foldListTree</code></p>
  </blockquote>