Scala concepts: Tail Recursion

In Scala, tail recursion enables you to rewrite a mutable structure such as a while-loop, into an immutable algorithm.

def fibonacci(n: Int): Int = {
  @scala.annotation.tailrec
  def go(i: Int, previous: Int, beforePrevious: Int): Int =
    if (i >= n) previous else go(i + 1, previous + beforePrevious, previous)

  go(i = 1, previous = 1, beforePrevious = 0)
}

assert(fibonacci(8) == 21)

Tail recursion always has a recursive call in a "final" position, ie you can only either return a result (exit the function), or return another call to self-function

In canonical form, the immutable form gets compiled down to the mutable form,

def evaluateGeneralImmutable[State, Result](initialParams: State)(
    iterate: State => State,
    terminate: State => Boolean,
    extractResult: State => Result
): Result = {
  @scala.annotation.tailrec
  def go(currentParams: State): Result =
    if (terminate(currentParams)) extractResult(currentParams)
    else go(currentParams = iterate(currentParams))

  go(initialParams)
}

becomes (after a stage of compilation):

def evaluateGeneralMutable[State, Result](initialParams: State)(
    iterate: State => State,
    terminate: State => Boolean,
    extractResult: State => Result
): Result = {
  var currentParams: State = initialParams
  while (!terminate(currentParams)) {
    currentParams = iterate(currentParams)
  }
  extractResult(currentParams)
}

This transformation can also be performed the other way round, as to give you a pure immutable solution

What are the benefits of tail recursion?

Tail recursion in Scala utilises a principle known as tail-call optimisation. It allows one to write iterative algorithms (that would otherwise would be complicated while-loops) in immutable form.

What are the benefits of immutability?

It becomes easier to reason about your code, and you always know that you can re-run a function as manytimes as you wish without causing unexpected side effects.

But really, can anything be written in this shape?

Anything that is iterative in nature can, so long as it can be represented in the canonical form.

Let's look at two versions of List#drop(n) - mutable and immutable (Drop, Take, dropRight, takeRight):

def dropMutable[T](list: List[T], n: Int): List[T] = {
  var remaining = n
  var returnList = list
  while (remaining > 0 && returnList.nonEmpty) {
    remaining = remaining - 1
    returnList = returnList.tail
  }
  returnList
}

def dropImmutable[T](list: List[T], n: Int): List[T] = {
  @scala.annotation.tailrec
  def go(remaining: Int, returnList: List[T]): List[T] = {
    if (remaining == 0) returnList
    else
      returnList match {
        case _ :: rest => go(remaining - 1, rest)
        case Nil       => Nil
      }
  }
  go(remaining = n, list)
}

assert(dropMutable(List(1, 2, 3), 2) == List(3))

assert(dropImmutable(List(1, 2, 3), 2) == List(3))

The key thing to notice really is that you move all the `var`s to arguments of the `go` function.

Very detailed advanced example:

Let's try to implement List#foldLeft (foldLeft and foldRight):

Example how to turn a function from mutable to immutable. Warning: a lot of boilerplate code.
def foldLeftMutable[T, S](list: List[T])(initial: S)(f: (S, T) => S): S = {
  var currentResult: S = initial
  var remaining: List[T] = list
  while (remaining.nonEmpty) {
    currentResult = f(currentResult, remaining.head)
    remaining = remaining.tail
  }
  currentResult
}

final case class CurrentState[S, T](
    currentResult: S,
    remainingItems: List[T]
)

def foldLeftMutableSimplified[T, S](
    list: List[T]
)(initial: S)(f: (S, T) => S): S = {
  var currentResult: CurrentState[S, T] =
    CurrentState(currentResult = initial, remainingItems = list)
  while (currentResult.remainingItems.nonEmpty) {
    currentResult = CurrentState(
      currentResult =
        f(currentResult.currentResult, currentResult.remainingItems.head),
      remainingItems = currentResult.remainingItems.tail
    )
  }
  currentResult.currentResult
}

def foldLeftCanonicalMutable[T, S](
    list: List[T]
)(initial: S)(f: (S, T) => S): S = {
  evaluateGeneralMutable(
    CurrentState(currentResult = initial, remainingItems = list)
  )(
    currentResult =>
      CurrentState(
        currentResult =
          f(currentResult.currentResult, currentResult.remainingItems.head),
        remainingItems = currentResult.remainingItems.tail
      ),
    _.remainingItems.isEmpty,
    _.currentResult
  )
}

def foldLeft[T, S](list: List[T])(initial: S)(f: (S, T) => S): S =
  evaluateGeneralImmutable[CurrentState[S, T], S](
    CurrentState(initial, list)
  )(
    iterate = currentState =>
      CurrentState(
        currentResult =
          f(currentState.currentResult, currentState.remainingItems.head),
        remainingItems = currentState.remainingItems.tail
      ),
    terminate = _.remainingItems.isEmpty,
    extractResult = _.currentResult
  )

def foldLeftInlined[T, S](list: List[T])(initial: S)(f: (S, T) => S): S = {
  type State = CurrentState[S, T]
  type Result = S

  val initialParams: State = CurrentState(initial, list)
  val iterate: State => State = currentState =>
    CurrentState(
      currentResult =
        f(currentState.currentResult, currentState.remainingItems.head),
      remainingItems = currentState.remainingItems.tail
    )
  val terminate: State => Boolean = _.remainingItems.isEmpty
  val extractResult: State => Result = _.currentResult
  @scala.annotation.tailrec
  def go(currentParams: State): Result =
    if (terminate(currentParams)) extractResult(currentParams)
    else go(currentParams = iterate(currentParams))

  go(initialParams)
}

def foldLeftInlinedFurther[T, S](
    list: List[T]
)(initial: S)(f: (S, T) => S): S = {
  type State = CurrentState[S, T]
  type Result = S

  @scala.annotation.tailrec
  def go(currentParams: State): Result =
    if (currentParams.remainingItems.isEmpty) currentParams.currentResult
    else
      go(currentParams = {
        val currentState = currentParams
        CurrentState(
          currentResult =
            f(currentState.currentResult, currentState.remainingItems.head),
          remainingItems = currentState.remainingItems.tail
        )
      })

  go(CurrentState(initial, list))
}

def foldLeftInlinedState[T, S](
    list: List[T]
)(initial: S)(f: (S, T) => S): S = {

  @scala.annotation.tailrec
  def go(currentResult: S, remainingItems: List[T]): S =
    if (remainingItems.isEmpty) currentResult
    else
      go(
        currentResult = f(currentResult, remainingItems.head),
        remainingItems = remainingItems.tail
      )

  go(initial, list)
}

def foldLeftCompact[T, S](list: List[T])(initial: S)(f: (S, T) => S): S = {

  @scala.annotation.tailrec
  def go(currentResult: S, remainingItems: List[T]): S =
    remainingItems match {
      case head :: tail =>
        go(currentResult = f(currentResult, head), remainingItems = tail)
      case Nil => currentResult
    }

  go(initial, list)
}