January 2019
Intermediate to advanced
322 pages
7h 29m
English
Recursion is heavily used in FP, as it is the canonical, and often the only, way to iterate. Functional language implementations will often include optimizations based on the so-called tail recursion (https://alvinalexander.com/scala/fp-book/tail-recursive-algorithms) to ensure that heavy recursion doesn't have a significant or excessive impact on memory consumption. Tail recursion is a particular instance of recursion where the return value of a function is calculated only as a call to itself. Here is a Scala example of calculating a Fibonacci sequence recursively. This first piece of code represents an implementation of the recursive function:
def fib(prevPrev: Int, prev: Int) { val next = prevPrev + prev println(next) if (next ...Read now
Unlock full access