title: why scala doesn't make tail call optimization?
body: <p>Just playing with continuations. The goal is to create function which will receive another function as parameter, and execution amount - and return function which will apply parameter given amount times.</p>
  
  <p>The implementation looks pretty obvious</p>
  
  <pre><code>def n_times[T](func:T=&gt;T,count:Int):T=&gt;T = {
    @tailrec
    def n_times_cont(cnt:Int, continuation:T=&gt;T):T=&gt;T= cnt match {
          case _ if cnt &lt; 1 =&gt; throw new IllegalArgumentException(s"count was wrong $count")
          case 1 =&gt; continuation
          case _ =&gt; n_times_cont(cnt-1,i=&gt;continuation(func(i)))
        }
    n_times_cont(count, func)
  }
  
  def inc (x:Int) = x+1
  
      val res1 = n_times(inc,1000)(1)  // Works OK, returns 1001
  
  val res = n_times(inc,10000000)(1) // FAILS
  </code></pre>
  
  <p>But there is no problem - this code fails with StackOverflow error. Why there is no tail-call optimization here? </p>
  
  <p>I'm running it in Eclipse using Scala plugin, and it returns
      Exception in thread "main" java.lang.StackOverflowError
          at scala.runtime.BoxesRunTime.boxToInteger(Unknown Source)
          at Task_Mult$$anonfun$1.apply(Task_Mult.scala:25)
          at Task_Mult$$anonfun$n_times_cont$1$1.apply(Task_Mult.scala:18)</p>
  
  <p>p.s. </p>
  
  <p>F# code, which is almost direct translation, is working without any issues</p>
  
  <pre><code>let n_times_cnt func count = 
      let rec n_times_impl count' continuation = 
          match count' with
          | _ when count'&lt;1 -&gt; failwith "wrong count"
          | 1 -&gt; continuation
          | _ -&gt; n_times_impl (count'-1) (func &gt;&gt; continuation) 
      n_times_impl count func
  
  let inc x = x+1
  let res = (n_times_cnt inc 10000000) 1
  
  printfn "%o" res
  </code></pre>