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…

17 1284 5

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:

Where Next?

Popular Backend topics Top

First poster: bot
Rust vs Go — Bitfield Consulting. Which is better, Rust or Go? Which language should you choose for your next project, and why? How do t...
0 1223 0
New
New
First poster: bot
In a previous post we talked about implementing a simple video chat with WebRTC and Elixir. This update will touch on some of the API cha...
1 1698 0
New
paulanthonywilson
Post on using UDP multicasting with Elixir to broadcast presence, and listen for peers, on a local network. I have found this approach us...
2 1198 0
New
paulanthonywilson
Following up on the previous post on using UDP multicasting to broadcast and detect peers on a network, I create a registry of those peer...
2 1706 0
New
CommunityNews
Functional programming is an increasing popular programming paradigm with many languages building or already supporting it. Go already su...
4 1197 1
New
brainlid
We take a deeper dive with Nathan Long into IOLists in Elixir. We cover what they are, how they work, the power they have when concatenat...
5 1098 0
New
axelson
I describe how we use Hot Reloading with Webpack to develop faster and show how to integrate Webpack 5, webpack-dev-server, and Phoenix f...
9 1108 3
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...
20 1573 13
New
brainlid
In episode 92 of Thinking Elixir, we talk with Mitchell Hanberg and learn about why he created the alternate Phoenix templating language ...
4 1062 0
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
476 5781 112
New
Devtalk
Reading something? Working on something? Planning something? Changing jobs even!? If you’re up for sharing, please let us know what you’...
1033 17470 383
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...
195 6396 95
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
49 5284 39
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
14 7927 8
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
160 3807 49
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
10 5256 6
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
32 3916 9
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...
0 4367 0
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
0 2781 0
New