Slide 1

Slide 1 text

Write stack safe non-tailrec recursive functions @jooohn1234

Slide 2

Slide 2 text

Jun Tomioka @jooohn1234 Software Engineer at M3, Inc.

Slide 3

Slide 3 text

Recursion is cool

Slide 4

Slide 4 text

Recursive data structures

Slide 5

Slide 5 text

Recursive functions

Slide 6

Slide 6 text

Recursive functions ● Describe WHAT it is rather than HOW it’d be done

Slide 7

Slide 7 text

Recursive functions ● Describe WHAT it is rather than HOW it’d be done Cool! FP Programmers

Slide 8

Slide 8 text

Stack Overflow

Slide 9

Slide 9 text

Recursive functions ● Tend to cause StackOverflowException

Slide 10

Slide 10 text

Tail recursion ● Make functions tailrec to avoid StackOverflow

Slide 11

Slide 11 text

Tail recursion ● Make functions tailrec to avoid StackOverflow Cool! FP Programmers

Slide 12

Slide 12 text

Tail recursion ● Make functions tailrec to avoid StackOverflow

Slide 13

Slide 13 text

Tail recursion ● Make functions tailrec to avoid StackOverflow It’s… cool! FP Programmers

Slide 14

Slide 14 text

Tail recursion? ● Make functions tailrec to avoid StackOverflow … but can we?

Slide 15

Slide 15 text

Tail recursion? ● Make functions tailrec to avoid StackOverflow … but can we?

Slide 16

Slide 16 text

Tail recursion? ● Make functions tailrec to avoid StackOverflow … but can we? It’s… still cool I believe! FP Programmers

Slide 17

Slide 17 text

Tail recursion? ● How many unique paths from S to G are there? S G

Slide 18

Slide 18 text

Tail recursion? ● Can you make this recursive function tailrec?

Slide 19

Slide 19 text

Tail recursion? ● Can you make this recursive function tailrec? ・・・ FP Programmers

Slide 20

Slide 20 text

Give up

Slide 21

Slide 21 text

Tail-recursion’s limitation ● It tends to be difficult to convert recursive functions that depend on results of multiple recursive calls.

Slide 22

Slide 22 text

Tail-recursion’s limitation ● It tends to be difficult to convert recursive functions that depend on results of multiple recursive calls.

Slide 23

Slide 23 text

Tail-recursion’s limitation ● It tends to be difficult to convert recursive functions that depend on results of multiple recursive calls. Monad! FP Programmers

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

The Monad version of recursion ● This function still describes the problem’s structure intuitively, doesn’t it? Cool! FP Programmers

Slide 26

Slide 26 text

The Monad version of recursion ● Of course just rewriting the recursive function with Monad doesn’t mean it is stack safe.

Slide 27

Slide 27 text

Trampoline

Slide 28

Slide 28 text

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.

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

Eval * We typically implement “Defer”, “Suspend” or something like that to avoid stack overflow when building the Eval.

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Eval * We typically implement “Defer”, “Suspend” or something like that to avoid stack overflow when building the Eval. Has following evaluation

Slide 33

Slide 33 text

Eval: trampoline

Slide 34

Slide 34 text

Eval: trampoline

Slide 35

Slide 35 text

[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

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Beautiful FP Programmers

Slide 39

Slide 39 text

Run with Id

Slide 40

Slide 40 text

Run with Eval

Slide 41

Slide 41 text

Run with Eval Cool! FP Programmers

Slide 42

Slide 42 text

Let’s implement stack safe foldRight with Eval

Slide 43

Slide 43 text

Let’s implement stack safe foldRight with Eval

Slide 44

Slide 44 text

Let’s implement stack safe foldRight with Eval !!! FP Programmers

Slide 45

Slide 45 text

Let’s implement stack safe foldRight with Eval This recursive call of loop causes stack overflow before stack safe trampoline evaluation

Slide 46

Slide 46 text

Use Defer to defer Eval instance construction

Slide 47

Slide 47 text

Use Defer to defer building Eval instance

Slide 48

Slide 48 text

Use Defer to defer building Eval instance Stack safe! FP Programmers

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

Thank you!