title: two dimensional tail recursion in scala
body: <p>I am new to <strong>Scala</strong> and started to learn about <strong>tail recursion</strong>. 
  I learned that tail recursion in <em>functional programming</em> is a counter part of iterations (for loops) in <em>imperative programming:</em></p>
  
  <p>Simple C++ loop to sum list elements:</p>
  
  <pre><code>uint32_t sum = 0;
  for (size_t i = 0; i &lt; list.length(); ++i) {
      sum += list[i];
  }
  </code></pre>
  
  <p>Scala recursive equivalent:</p>
  
  <pre><code>def listSum(list: List[Int]): Int = {
    def listSumHelper(list: List[Int], sum: Int): Int = {
      if (list.isEmpty) sum
      else listSumHelper(list.tail, sum + list.head)
    }
    listSumHelper(list, 0)
  }  
  </code></pre>
  
  <p><strong>Question:</strong>
  What would be a scala recursive equivalent of nested for loops ?</p>
  
  <pre><code>uint32_t sum = 0;
  for (size_t i = 0; i &lt; list.width(); ++i) {
      for (size_t j = j &lt; list.height(); ++j) {
          sum += list[i][j];
      }
  }
  </code></pre>