elbrujohalcon

elbrujohalcon

How do List Functions Fail in Erlang?

A long time ago, I wrote an article about The Asymmetry of ++, thanks to
Fede Bergero’s findings. Let’s add a few more asymmetries to that list…

Most Liked

OvermindDL1

OvermindDL1

As to the originally referenced article, ++ isn’t asymmetric in the way it was shown but rather it’s a function that would be written like this in elixir:

def ++(left, right), do: append(:lists.reverse(left), right, [])
def shift_cells([], acc), do: acc
def shift_cells([e|r], acc), do: [e, acc]

Which is precisely what it is defined to do. Lists on the beam in erlang and elixir are not typed lists, they are not full “proper” Cons lists, you can potentially make lists in the first Cons element, the second, zig zag every which way, etc… The ending element isn’t special, it doesn’t need to be a list. Now sure ++ would not make much sense in a statically typed language, but erlang/elixir are not statically typed languages. It’s not an asymmetry as it is not a ‘prepend’ operator, it is more of a ‘shift cells over’ operator.


As for this article, looks good. ^.^

You really should put what OTP version you were working with in Erlang as a lot of error responses for BIF’s have changed in recent versions (more information in the exceptions! ^.^).

I wouldn’t opt for the exception catching of the lists calls but rather a pre-check, or toss the check ‘up’ the callstack by requiring, for example, the list argument to your function to be List=[_|_] instead of just List to enforce a Cons cell instead of a Nil cell.

Basically, behave like lists:foldl/3 but don’t treat empty lists as a special case.

I’m not sure I agree, those are different issues with different exceptions. More I would argue that the exceptions should not be caught at all to begin with as malformed input was supplied to the function and thus who knows what other bad data there is, this is part of OTP’s Let It Crash philosophy, and exceptions are indeed “exceptional” events, not for standard control flow like they are being used here. Plus adding those guards may seem easy to something like map, but that is going to incur a cost on one of the hottest code paths in the entire system, not sure it’s worth it (although with the new JIT in OTP24, who knows, benchmark?).

OvermindDL1

OvermindDL1

Hear hear! I really like the recent changes. ^.^

Lol, why do I want a link to this discussion? ^.^

In this case yes. Most good type systems can enforce non-emptiness, and erlang likes to pretend it does as well. It’s on the caller to ensure they are passing in good data in that case. I’m a fan of static typed systems that can actually enforce this though, lol. Dialyzer helps a little bit at least. Hmm, does dialyzer catch that case actually?

elbrujohalcon

elbrujohalcon

Thank you for the super-detailed answer(s), @OvermindDL1 !!

I should’ve stated that I was testing this on OTP23, you’re right.

In any case, Lukas Larsson (from the OTP team) already replied in Medium with that regarding what they’re doing to improve error descriptions… and it’s GREAT!!

Finally, to some things in your message…

100% agree! I actually had to explain this very same thing when discussing the robot butt article on lobsters recently.

To be clear: Are we talking about the same different issues here? What I tried to say was that calling a function that works normally with empty lists, with an empty list and another wrong argument, should not behave as if it was called with something that’s not a list. Instead, it should behave as if it was called with a non-empty list. Do you still think that calling it with a bad fun and an empty list is a different issue than calling it with a bad fun and a non-empty list?

Of course, you’re right. But I was exemplifying.

Yeah, I agree again. That’s why the section in the article is called Is this a Problem? and not This is a Problem. This is clearly a made up problem just for the sake of arguing, except for the confusing error descriptions in the shell, which is what Lukas and the OTP Team are fixing right now :tada: :exclamation:

Popular Backend topics Top

AstonJ
This article was written by @rvirding …over a decade ago! Posting here in case anyone else finds it of interest and adding it to our Erla...
New
New
AstonJ
If you’re interested in Rust this is worth a read :smiley: Technology from the past come to save the future from itself Hi I have be...
New
paulanthonywilson
So you’re enjoying using WebSockets with Elixir’s Phoenix Framework, and you want to send some binary messages. Maybe it’s an audio clip,...
New
First poster: malloryerik
Everyone outside of tech has heard of JavaScript, Java, Python, Ruby and even .Net, but few if any have heard of F#. However, F# may be o...
New
First poster: bot
Once a year, I look back at the recent developments in the PHP world, and also look forward to what’s to come. And just like in 2020 and ...
New
First poster: bot
Today we’ll just talk about integer casting, but Zig also provides explicit casting support for floats, bools, enums, pointers, and more....
New
First poster: bot
This post is a spiritual successor to Loris Cro’s Go cross-compilation. The encounter During a recent stage 2 meeting Jakub Konka wanted...
New
First poster: bot
Our blog has had a long standing interest in novel uses of the BEAM, or Erlang virtual machine, as shown by the many articles we have pub...
New
First poster: AstonJ
Ruby’s Struct is one of several powerful core classes which is often overlooked and under utilized compared to the more popular Hash clas...
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
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
dasdom
No chair. I have a standing desk. This post was split into a dedicated thread from our thread about chairs :slight_smile:
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol: bre...
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
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New

Latest in Blogs/Talks

View all threads ❯