Skip to content

Instantly share code, notes, and snippets.

@edisonywh
Created November 30, 2018 09:18
Show Gist options
  • Save edisonywh/410948ece3b55953870330a0937aa9da to your computer and use it in GitHub Desktop.
Save edisonywh/410948ece3b55953870330a0937aa9da to your computer and use it in GitHub Desktop.

Recursion, Tail Call Optimization and Recursion.

Recently I thought I'd take a jab at functional languages just to try out a different paradigm of thinking, and I decided to pick up Elixir since it's so syntatically similar to Ruby. While I was learning, I found something.

TLDR:

  • Elixir has no loops
  • Recursion makes an additional call to call stack (which is why your stack overflows if you forget your base case)
  • Tail Call Optimizations is a compiler feature that optimizes recursions (if the last thing it does is call itself)!

There is no concept of loop in Elixir.

Yes, none of your for loops or .each. It's a bit of a strange concept at first, but let's see what's the alternative.

If you can't use an iterative approach to iterate a loop, how do you traverse a list? Recursion.

Brief recap: Recursion is when a function calls itself, [like this](link to the current page)

A Ruby Example:

https://gist.github.com/0929c2a22dcce9ee4fc30b32103b64ac

An Elixir Example:

https://gist.github.com/c9e223be2f127c2f8e19fa920192f9b7

Pretty interesting to traverse recursively instead of the iteratively aye?

LARGE NUMBERS!

Next, let's see what happens when it comes to a large number!

Ruby:

https://gist.github.com/5662dd4b945a6c292c969ac8544345ba

It crashes with a SystemStackError! Wow, does that mean recursion is bad? does that mean recursion is bad? does the mean recursion is bad? does that mean recursion is ... Okay, before we go crazy, let's take a look at how Elixir deals with it.

Elixir:

https://gist.github.com/d060f4790172c31b10330df09f2a558c

You can see that Elixir effortlessly handles all five hundred thousands recursion. But why does it work in Elixir but not Ruby?

Because of Tail Call Optimization!

What is Tail Call Optimization?

Tail Call Optimization (TCO) is a compiler feature in which the compiler automatically optimizes the stack, if the last thing that a function does is call itself.

But what is the stack? and what do you mean by 'optimizes the stack'?

Stack is a data structure that employs the LIFO mindset (Last-In-First-Out), think of it as a fixed array but you can only insert from one end and take out from the same end. What we're specifically talking about here is the call stack.

To understand this better, we need to first understand how call stacks work.

When you call a function, it gets added to the call stack, and when it finish executing, it gets popped off the stack, like so:

[Insert Call Stqck]

And when you have a recursive function call, it's basically adding itself to the call stack every single time. If you forget your base case/have too big of a call stack, it throws a stack overflow error! (what we saw earlier), like this.

[Insert Recursion Stack]

All TCO means is that, when the compiler sees the last call of the function is calling itself, instead of adding the function call to the call stack, it does a goto instead, and basically restarts the same function call, requiring O(1) instead of O(n) space (we will prove this later).

Now this is what it looks like when it's TCO-ed:

[Insert TCO Stack]

Do we have that in Ruby?

We do indeed! However TCO is not enabled by default, so you have to pass in the compile option during runtime, and you can do it like so:

https://gist.github.com/60c7d4841e91780cafd64d5145e792f1

Not very useful if you have to enable it with a flag, but it's there if you need it!

Proof?

Got your back! In RubyLand, We can inspect the current call stack with a call to Kernel#caller. Lets .count!

Change our code like so:

https://gist.github.com/42b291d65476e9c60e34fb6557cb78a6

Conclusion

Alright so that's it for today! Thought it's pretty cool that I'm trying to learn Elixir but end up learning something about compiler instead!

Check out my previous articles too:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment