CommunityNews

CommunityNews

Tail-call optimization in Elm

What is TCO?

Tail-call optimization (TCO) is a very neat trick that the Elm compiler does to make recursive functions a lot more performant and stackoverflow-proof.

Evan Czaplicki describes it very well in this article and I recommend you go read it. He calls it tail-call elimination but it’s a different name for the same thing.

To summarize Evan’s article, a “tail-call optimized” function is a recursive function that gets compiled to using a loop instead of function calls to itself. Let’s take the following code as an example…

Read in full here:

This thread was posted by one of our members via one of our news source trackers.

Most Liked

OvermindDL1

OvermindDL1

That’s not TCO that Elm does, it’s self-call tail recursive optimization, it only can happen in fully known contexts and is not general enough to be full TCO. Full Tail Call Optimization is where a call is in the tail-most position of a function, that means you can then compile essentially a goto to it (or in the Rust proposal a become for example), this means the call can be dynamically made, can be any amount deep through any amount of function stacks, etc… etc… I really don’t like how the Elm community keeps trying to misrepresent things about the Elm language, it’s an annoying recurring pattern…

In fact, let’s try it, I go to: Try Elm!
And I try running:

import Html exposing (text)

entry f i acc = if i <= 0 then acc else f (i - 1) (acc + i)

dispatch1 i acc = entry dispatch2 i acc

dispatch2 i acc = entry dispatch1 i acc

main =
  let i = dispatch1 10000 0 in
  text (String.fromInt i)

And the result is:

Initialization Error

InternalError: too much recursion

Well, let’s try this in Elixir, a language that DOES support TCO, so the same code ported running through the repl:

❯ iex
Erlang/OTP 24 [RELEASE CANDIDATE 3] [erts-12.0] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit]

Interactive Elixir (1.12.0-rc.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> defmodule TestingTCO do
...(1)>   def entry(f, i, acc), do: if(i <= 0, do: acc, else: f.(i-1, acc+i))
...(1)>   
...(1)>   def dispatch1(i, acc), do: entry(&dispatch2/2, i, acc)
...(1)>   
...(1)>   def dispatch2(i, acc), do: entry(&dispatch1/2, i, acc)
...(1)> end
{:module, TestingTCO,
 <<70, 79, 82, 49, 0, 0, 7, 176, 66, 69, 65, 77, 65, 116, 85, 56, 0, 0, 0, 202,
   0, 0, 0, 20, 17, 69, 108, 105, 120, 105, 114, 46, 84, 101, 115, 116, 105,
   110, 103, 84, 67, 79, 8, 95, 95, 105, 110, ...>>, {:dispatch2, 2}}
iex(2)> TestingTCO.dispatch1(10000, 0)
50005000

And since Elixir really does implement TCO and not just a simple recursive loop optimization unlike elm’s communities repeating lies then we can go way way higher!

iex(3)> TestingTCO.dispatch1(1000000, 0)
500000500000

Elm has a lot of issues both as a language and as a community, and passing off a very very common optimization pass performed in almost every language as TCO is just the tip of the iceberg…

AstonJ

AstonJ

Even if it’s not true TCO, performance enhancements are always welcome when it comes to JS land :nerd_face:

rustkas

rustkas

I have already read a lot about the disadvantages of Elm. In your opinion, what is currently better than Elm (in terms of the quality of code generation, what to choose for creating a new application) for the front end part?

Popular Frontend topics Top

AstonJ
This is a really good article explaining what WebAssembly is and how to get started with it - well worth a read! Pinning it for our /was...
New
First poster: bot
Just one year before the first web page went live in 1991, Microsoft began shipping perhaps the most well-known icon font, Wingdings. How...
New
First poster: bot
Add WebAssembly, get performance. Is that how it really works? The incredibly unsatisfying answer is: It depends. It depends on oh-so-ma...
New
First poster: bot
ES2021 features list as approved by the Ecma General Assembly Logical Assignment Operators (&amp;&amp;= | Numeric Separators (1_000) ...
/js
New
First poster: bot
Let’s build a fully functioning and settable “analog” clock with CSS custom properties and the calc() function. Then we’ll convert it int...
New
brainlid
In episode 59 of Thinking Elixir, we talk with Joel Kemp about his experience introducing Elixir at Spotify. We learn about the concurren...
New
AstonJ
I love videos like this and it follows the same theme as @jaeyson’s thread here - I reckon the @ElixirCasts crew should consider experime...
New
AstonJ
I can’t remember who was asking about CSS tuts now… but these just showed up on my YouTube feed and look pretty good/up to date :023:
New
First poster: bot
Star Wars Scene Transition Effects in CSS. You know those wipe transitions between scenes in Star Wars movies? Have you ever thought it ...
New
sundi
Learn to customize the Error HTML module in Phoenix LiveView, while enhancing the UX and retaining the typed URLs on branded 404 pages. ...
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1134 25456 753
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New