title: Reverse list Scala
body: <p>Given the following code:</p>
  
  <pre><code>import scala.util.Random
  
  object Reverser {
  
    // Fails for big list
    def reverseList[A](list : List[A]) : List[A] = {
      list match {
        case Nil =&gt; list
        case (x :: xs) =&gt; reverseList(xs) ::: List(x)
      }
    }
  
    // Works
    def reverseList2[A](list : List[A]) : List[A] = {
      def rlRec[A](result : List[A], list : List[A]) : List[A] = {
        list match {
          case Nil =&gt; result
          case (x :: xs) =&gt; { rlRec(x :: result, xs) }
        }
      }
      rlRec(Nil, list)
    }
  
    def main(args : Array[String]) : Unit = {
      val random = new Random
      val testList = (for (_ &lt;- 1 to 2000000) yield (random.nextInt)).toList
      // val testListRev = reverseList(testList) &lt;--- Fails
      val testListRev = reverseList2(testList)
      println(testList.head)
      println(testListRev.last)
    }
  }
  </code></pre>
  
  <p>Why the first version of the function fails (for big inputs), while the second variant works . I suspect it's something related to tail recursion, but I am not very sure . Can somebody please give me "for dummies" explanation ?</p>