Scala tail-recursion
Recursion is a technique defined as using a function or method that calls itself again and again, until it solves the problem.
The recursion technique helps us solve simple problems very easily and even makes it possible to solve complex problems. It is easy to reason about and needs a lot less code, as compared to the iterative approach.
What kind of problems can we solve using the recursion technique? Any problem that is defined in terms of itself.
Types of recursions
We can implement recursion in different ways. However, we are considering only the following two types:
- Linear recursion
- Tail-recursion
Tail-recursion is one form of recursion technique. We can say a recursive call is tail-recursive, when a recursive call is the last thing executed by that function and it does nothing after that, just returns its value.
Benefits of linear recursion
Unlike non-recursive approaches like loops, which use mutable variables, it supports immutability:
- We can write more readable code
- It...