tovarchristian21

tovarchristian21

Optimized Tail Recursion

Hello guys, I’ve been wondering lately how do I actually know my recursive functions are in fact optimized and every recursive call is not generating a new stack. Is the arity of the function enough in order to know that a new stack has not been generated?

Thanks in advance.

Marked As Solved

Qqwy

Qqwy

TypeCheck Core Team

The tl;dr is: A function is tail recursive if the recursive call is the last thing the function does. If it first calls itself recursively and after that still does something else with the result, then it is not tail recursive and cannot be optimized away.

Also interesting is Tail Recursion is not a silver bullet which talks about Elixir/Erlang’s memory model (stack and heap growing towards each-other) which for many situation makes tail-recursion slightly less important than in some other languages.
Situations in which it remains very important are:

  • When reducing streams (whose length you don’t know; they might very well be more than can fit in memory).
  • When writing handler functions for processes. (if these are not tail-recursive, then the process will start hogging up more and more memory as time goes on). Note that when using GenServer-based processes, this is already managed for you behind the scenes.

Also Liked

rvirding

rvirding

Creator of Erlang

Just to point out that this optimisation not just done for tail-recursive calls but it is done for every tail-call. It is a TCO.

shanesveller

shanesveller

One of the simplest pitfalls that someone might intuitively write and end up breaking TCO is to include an operator outside of the final recursive call, but still on the final line, such as:

def recursive(0), do: 0

def recursive(n) when n > 1, do: 1 + recursive(n - 1) 

I’ve done it myself and see it in others’ code I’ve reviewed.

As a fun aside, if you’re on OSX, you can use time -l ... when running any shell command to observe small details about a limited set syscalls as well as memory allocations during the execution, and you can see that the above style uses more and more memory the larger argument you give it, but a properly TCO’d function will grow memory usage by argument size much more slowly, but still non-zero.


It doesn’t translate perfectly because our runtimes have such different semantics, but Haskell’s wiki article about their versions of fold-from-left and fold-from-right has some interesting insights here too, and I think the “visualization” they offer by expanding the math functions captures what I’m trying to express above pretty well:

https://wiki.haskell.org/Foldr_Foldl_Foldl'

foldr (+) 0 [1..1000000] -->
1 + (foldr (+) 0 [2..1000000]) -->
1 + (2 + (foldr (+) 0 [3..1000000])) -->
1 + (2 + (3 + (foldr (+) 0 [4..1000000]))) -->
1 + (2 + (3 + (4 + (foldr (+) 0 [5..1000000])))) -->
-- ...
-- ...  My stack overflows when there's a chain of around 500000 (+)'s !!!
-- ...  But the following would happen if you got a large enough stack:
-- ...
1 + (2 + (3 + (4 + (... + (999999 + (foldr (+) 0 [1000000]))...)))) -->
1 + (2 + (3 + (4 + (... + (999999 + (1000000 + ((foldr (+) 0 []))))...)))) -->

1 + (2 + (3 + (4 + (... + (999999 + (1000000 + 0))...)))) -->
1 + (2 + (3 + (4 + (... + (999999 + 1000000)...)))) -->
1 + (2 + (3 + (4 + (... + 1999999 ...)))) -->

1 + (2 + (3 + (4 + 500000499990))) -->
1 + (2 + (3 + 500000499994)) -->
1 + (2 + 500000499997) -->
1 + 500000499999 -->
500000500000
dimitarvp

dimitarvp

AppSignal’s article is explaining it quite okay.

NobbZ

NobbZ

You know this by writing your functions in a way that is optimisable.

The much bigger problem for some seems to see, which function is actually called last…

Though I’m missing words to explain right now.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement