What is Recursion tail in Scala?

Category : Scala | Sub Category : Scala Interview Questions | By Prasad Bonam Last updated: 2023-09-27 00:32:54 Viewed : 268


What is Recursion tail in Scala?

In Scala, a tail recursion is a specific form of recursion in which the recursive call is the last operation performed within a function before it returns a result. In other words, the recursive call is in the "tail position" of the function. This is significant because tail-recursive functions can be optimized by the Scala compiler into more efficient iterative code, eliminating the risk of stack overflow errors that can occur with regular (non-tail) recursion.

To make a function tail-recursive in Scala, you typically use an accumulator variable to accumulate the result as you make the recursive calls. This ensures that no additional work is left to be done after the recursive call returns, allowing the compiler to perform tail-call optimization (TCO).

Here is an example of a tail-recursive function to calculate the factorial of a number:

scala
import scala.annotation.tailrec object Factorial { def factorial(n: Int): Int = { @tailrec def factorialHelper(n: Int, accumulator: Int): Int = { if (n == 0) { accumulator } else { factorialHelper(n - 1, n * accumulator) } } factorialHelper(n, 1) } def main(args: Array[String]): Unit = { val result = factorial(5) println(s"Factorial of 5 is $result") } }

In this example, factorialHelper is the tail-recursive function that calculates the factorial of a number. It takes two parameters: n (the current number for which the factorial is being calculated) and accumulator (the accumulated result). The @tailrec annotation indicates to the Scala compiler that this function should be optimized for tail recursion.

By accumulating the result using the accumulator parameter and making the recursive call in the tail position, this function can be optimized by the compiler to use a loop internally, preventing stack overflow errors for large inputs.

Tail recursion is an important concept in functional programming and is widely used in Scala for writing efficient and stack-safe recursive algorithms.


Tail recursion in Scala is a special form of recursion in which the recursive call is the last operation executed within a function. This tail-recursive structure allows the Scala compiler to optimize the function, turning it into an iterative loop, which can prevent stack overflow errors for deep recursion. To create a tail-recursive function, you need to ensure that the recursive call is in the tail position and that no further computation is performed after the call.

Here is an example of a tail-recursive function to calculate the factorial of a number:

scala
import scala.annotation.tailrec object Factorial { // Define a tail-recursive factorial function @tailrec def factorial(n: Int, accumulator: BigInt = 1): BigInt = { if (n <= 1) { accumulator } else { factorial(n - 1, n * accumulator) } } def main(args: Array[String]): Unit = { val result = factorial(10000) println(s"Factorial of 10000 is: $result") } }

In this example:

  • We import the scala.annotation.tailrec annotation to indicate that we intend to make the factorial function tail-recursive.

  • The factorial function takes two parameters: n (the number for which we want to calculate the factorial) and accumulator (an accumulator for accumulating the result).

  • Inside the function, we check if n is less than or equal to 1. If it is, we return the accumulator, which is initially set to 1. This serves as the base case for the recursion.

  • If n is greater than 1, we make a recursive call to factorial, passing n - 1 as the new value of n and n * accumulator as the updated accumulator. This ensures that the recursive call is in the tail position.

  • In the main method, we call factorial with an input of 10,000, which would lead to a deep recursion if not optimized for tail recursion.

When you run this code, it will calculate the factorial of 10,000 without causing a stack overflow error, thanks to the tail-recursive optimization:

csharp
Factorial of 10000 is: 284625968091705451890641321211986889014805140170279923079417999427441134000376444377299078675778477581588406214231752883004233994015351873905242116138271617481982419982759241828925978789812425312059465996259867065601615720360323979263287367170557419759620994797229089473540387051618229687571057161167484243359928466508244718680677827052584811254069848063287845142560523490865850786646938780243634655558146946091083461122746531886806098387439230074656565542920243648667790904186492204341745323632849623304642106613620022017578785185740916205048971178182040018728293994344618622432800983732376493181478984811945271300744686202399044446397654300904234829763604820737068333950607583155057694831557253993180060342948389610475937252428388642865656383612794241051817837907943516292361934105813164033008546034034723254517283943063903630525882475028839194989650413718679669752150525024567041855602203380694858444052670662653487144937217411783622137091919574564396045599059400722578409032637160538307870116535854046997788810172001252026008201512382891242380109552060596380044947167778899763004750451263031325311291082788660902559992569967519376833877780153671546314042733389042753327327000095777970808697191600002986490915950532285791217634998685541167095080736059465387257050743789156756353836187354062939904569798034857732700892746169041101910826504940871287019573383977304408341321039150889244594794617050461022961570862792385627576111231666885237125047870908915522417189585361680446755757308513366836011252305854686145701924968017786040868489575714659240690702542821683118608276303179755195044806408654815244273485807574323508024674840607917019158748353517490088160829946589101067473885323153937073549534480572414374753421618597523121599961918959431237386307073759906877248935144503335722836868371767614346007472174206122297668677245384092264648048337671163625059521175820976333565008058889618201546979610062594845569034711972996409089418059534393251085263122144583995501944651161493093757096855455938151864288906662144927536940232861630207007147936462063542225159870224897262918553842225858306408301661630823780820188145152174054919057637624709319831035847091091942736167889046386076267586514175386977831449542288985731971295437295516778477581588889133799487538352045005986982524769103063434825539761648209318374414978068609242332051120709607482043609919639048096534799153539226613464520568415674943405557186864158093290035694888528519040295604734949687792393478964036001572651121003296678424115924568470408964270070544509962884079903799069910235737143245007998702956650302619278844909212278855394878353830564686488165556229431567312827439082645240818138407221002694929874736544333161830701144898882677797199582767810646219613894165991647922111729083745045060895662852929614166176410127239150444058680468681562470014614041760181144565253579376840544810374632277416630600202042399837058442618443252513344531262777630999894419552502727488874144225155309678572709432186106708524508487603405671849711942291933458153099161283416370145196312327869579036212676800006442806906218287287675734397007561783949314102578214685925108372079370298219289760071984038509624554443629812309878799272442


Search
Related Articles

Leave a Comment: