Recrusive function is cool, but it sometimes cause stack overflow in naive implementation. I'll explain how to write any recursive functions in stack safe way.
Tail-recursion’s limitation ● It tends to be difficult to convert recursive functions that depend on results of multiple recursive calls. Monad! FP Programmers
Trampoline ● > a trampoline is a loop that iteratively invokes thunk-returning functions ○ https://en.wikipedia.org/wiki/Trampoline_(computing) #High-level_programming ○ “Trampoline” in computer science has a lot of meanings. This is just one of them.
Trampoline ● A trampoline loop evaluates values one by one until it returns final result. ● We can write this evaluation loop as tailrec! Eval evaluate Eval Result OR loop
[Optional] Memoization ● This is not the essence, but memoization to reduce running time ● In many cases, we can make such “What it is” style of recursive functions faster by using memoization
Recap ● Recursive functions is cool especially when it describes ”what” rather than ”how” ● We can apply the trampoline technique to avoid stack overflow when tail-recursion is not effective