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?

Where Next?

Popular Frontend topics Top

First poster: bot
The Zen of index.html. Web development has become a very complex field with many branches and tools to master. In this article, I sugges...
New
First poster: bot
In JavaScript programs, asynchrony arises in situations such as web-based user-interfaces, communicating with servers through HTTP reques...
/js
New
First poster: bot
JavaScript allows calling a function with a different number of arguments than the expected number of parameters, i.e., one can pass fewe...
/js
New
CommunityNews
As JavaScript developers, the sheer amount of stuff we’re expected to know is enormous, and it grows bigger every day. With so many diff...
New
First poster: bot
Decided to write this blog post because I couldn’t find any documentation or online examples like the one I’m sharing here. Every example...
New
First poster: bot
The TypeScript type system is immensely powerful, and while the language itself is, of course, Turing complete, so is the type system its...
New
First poster: bot
JavaScript has come a long way since I knew it as the “D” in DHTML. For anyone like me, who’s been reluctant to use the latest syntax tha...
/js
New
First poster: rustkas
What is TCO? Tail-call optimization (TCO) is a very neat trick that the Elm compiler does to make recursive functions a lot more performa...
New
First poster: bot
In this article, we will look at the fascinating evolution of graphics in browsers from the prehistoric days of the early browsers. We wi...
/js
New
First poster: OvermindDL1
I have to admit: as I’ve watched Tailwind enthusiastically adopted by more and more of the frontend community, I’ve remained skeptical. B...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
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
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
First poster: bot
The overengineered Solution to my Pigeon Problem. TL;DR: I built a wifi-equipped water gun to shoot the pigeons on my balcony, controlle...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New