Skip to main content

Posts

Showing posts from November, 2013

Tail recursion in Scala

Today I learned that tail recursion optimization can really solve the classic problem of stack overflow in recursive functions that depend on data size this I saw by writing a simple sum function in Scala that sums up a list val x = (1 to 3).toList // simple recursive function def sum(x: List[Int]): Int = {    if (x.isEmpty) 0   else x.head + sum(x.tail) } ok so far so good, this works, and return 6 for the simple list of size 3. But change the 3 to 300000 and you will see a stackOverFlow in the Scala workspace (you save the workspace and Scala will re compute). This is because a new frame is added to the stack for each recursive call and soon that exceeds the stack limit. How do you solve this? Tail recursion comes to the rescue. The term applies to functions whose last call is actually a self call, without any other computations. Scala compiler will internally then convert such functions into a while loop with goto, instead of pure recursion, hence avoiding