Jun Tomioka
June 29, 2019
760

# Write stack safe non-tailrec recursive functions

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.

June 29, 2019

## Transcript

it’d be done
7. ### Recursive functions • Describe WHAT it is rather than HOW

it’d be done Cool! FP Programmers

11. ### Tail recursion • Make functions tailrec to avoid StackOverﬂow Cool!

FP Programmers

13. ### Tail recursion • Make functions tailrec to avoid StackOverﬂow It’s…

cool! FP Programmers

but can we?

but can we?
16. ### Tail recursion? • Make functions tailrec to avoid StackOverﬂow …

but can we? It’s… still cool I believe! FP Programmers
17. ### Tail recursion? • How many unique paths from S to

G are there? S G

19. ### Tail recursion? • Can you make this recursive function tailrec?

・・・ FP Programmers

21. ### Tail-recursion’s limitation • It tends to be diﬃcult to convert

recursive functions that depend on results of multiple recursive calls.
22. ### Tail-recursion’s limitation • It tends to be diﬃcult to convert

recursive functions that depend on results of multiple recursive calls.
23. ### Tail-recursion’s limitation • It tends to be diﬃcult to convert

recursive functions that depend on results of multiple recursive calls. Monad! FP Programmers
24. ### The Monad version of recursion • This function still describes

the problem’s structure intuitively, doesn’t it?
25. ### The Monad version of recursion • This function still describes

the problem’s structure intuitively, doesn’t it? Cool! FP Programmers
26. ### The Monad version of recursion • Of course just rewriting

the recursive function with Monad doesn’t mean it is stack safe.

28. ### 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.
29. ### Trampoline • A trampoline loop evaluates values one by one

until it returns ﬁnal result. • We can write this evaluation loop as tailrec! Eval evaluate Eval Result OR loop
30. ### Eval * We typically implement “Defer”, “Suspend” or something like

that to avoid stack overﬂow when building the Eval.
31. ### Eval * We typically implement “Defer”, “Suspend” or something like

that to avoid stack overﬂow when building the Eval. Represents single value
32. ### Eval * We typically implement “Defer”, “Suspend” or something like

that to avoid stack overﬂow when building the Eval. Has following evaluation

35. ### [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

43. ### Let’s implement stack safe foldRight with Eval This recursive call

of loop causes stack overflow before stack safe trampoline evaluation

Programmers
47. ### Recap • Recursive functions is cool especially when it describes

”what” rather than ”how” • We can apply the trampoline technique to avoid stack overﬂow when tail-recursion is not effective