1. 21
  1.  

  2. 18

    TLDR: Instead of using length(list) > 0 - which traverses the whole list - use list.is_empty() which in Erlang you can implement using pattern matching with [_|_] which looks like a robot butt.

    1. 2

      With help from Filipe Varjão in Twitter, I now added a TL;DR directly into the article.

      Nobody can say that I don’t listen to my readers :P

      1. 1

        I read the article thinking I was going to learn some obscure new fact only to find that it was explaining a simple concept I knew in a far more complicated way. This TLDR comment is better explanation.

        Also, why, in a blog post, are single letter variables used? I can understand using them in the shell for experimentation but in a blog post they only make the Erlang syntax more obscure and hard to understand. We should be trying to make Erlang syntax as readable as possibly by using VeryDescriptiveVariableNames.

        1. 3

          I read the article thinking I was going to learn some obscure new fact only to find that it was explaining a simple concept I knew in a far more complicated way.

          I think you put too much trust in my blog writing… and I appreciate that :)

          This TLDR comment is better explanation.

          Of course, since the article is not just an explanation. It’s also a joke and a funny story and a rumbling… or a combination of all that.

          Also, why, in a blog post, are single letter variables used? […] VeryDescriptiveVariableNames.

          Because I’m super-lazy and the variable names were not the point of the article. But don’t worry, I went in and extended the variable names now, they are NotSuperVeryClearYet, but it doesn’t really matter… Nobody looks at the variables when there is a super-sexy robot butt in front of them ;)

        2. 1

          I don’t think you can use a user-defined list.is_empty in guards, though.

          1. 1

            You can’t, you can match against [], tho.

            1. 1

              I’m a little disappointed erlang doesn’t have is_nonempty_list/1 which would play nicely with dialyzer, too.

              You can build such a guard in elixir:

              defguard is_nonempty_list(l) when is_list(l) and l != []
              

              Likewise disappointed it’s not in the Kernel module

          2. 1

            Why does Erlang not carry this information as a class attribute?

            1. 7

              Because lists are not a class, in Erlang. As a matter of fact, lists are just a human construct in Erlang. The language only provides a basic element with no particular meaning ([]) and a way to link two things together: The cons operator ( […|…]). The standard library (OTP) provides a module to use that cons operator to build lists by always putting a list on the second element of it and understanding [] as the empty list. So… if you use the functions in the lists module, it’s assumed that you will pass lists (i.e. things constructed by applying cons 0-N times over []), but in reality… nothing prevents you from using lists functions on things that are not actually lists…

              1> lists:sublist([a | [b | [c | []]]], 1, 2).
              [a,b]
              2> lists:sublist([a, b | [c]], 1, 2).
              [a,b]
              3> lists:sublist([a,b,c], 1, 2).
              [a,b]
              4> lists:sublist([a,b|{something, that, is, "not a list"}], 1, 2).
              [a,b]
              

              In a nutshell: [] and […|…] are basic building blocks of Erlang. Everything else (e.g. […, …, … | …], […, …, …], etc.) is just syntactic sugar.

          3. 8

            I know it’s off-topic because the article is great but names like “robot butt” make me wince and/or cringe, just feel a little gross.

            1. 12

              Then you will love The Goatse Operator =()=!

              1. 3

                OH NO

              2. 1

                Sorry about that, not a native speaker myself =/

                1. 11

                  Should’ve been robutt

                  1. 10

                    I’m a native speaker and I think “robot butt” is hilarious. Humor is subjective. I’ll grant that “robot butt” is perhaps somewhat unprofessional, and if I worked in an Erlang shop I might not share this with my coworkers for that reason, but don’t take the criticism too personally.

                2. 3

                  Or you could just test if the list is [].

                  1. 1

                    Sure, and that’s mentioned in the article. That said, matching ‘not the empty list’ also means the possibly matching things that aren’t lists. Fortunately, there’s also the is_list/1 BIF that’s allowed in guard statements. Once learned, though, the robot-butt is, in my opinion, easier to read than L when is_list(L)

                    The robot-butt is fun, and it showcases pattern matching, one of the things that make programming Erlang so much fun.

                    1. 1

                      I think it’s a game of semantics vs implementation details. I find [] for empty list to be more correct, but I guess in /most/ functional languages it is actually [Head | Tail] where Tail can be empty… Maybe the idiomatic way of [] vs [_|_] is different per language…

                      1. 1

                        I would be surprised if any functional language permitted [Head | Tail] for empty list.

                        1. 1

                          [] is definitely the most correct way to match an empty list.

                          [Head | Tail], as well as [_ | _], will never match an empty list in Erlang; they will only ever match (possibly improper) lists of 1 or more elements.

                          1. 1

                            I guess I phrased it weirdly, my bad. Of course it was meant to be “an empty list for Tail”, which would make the complete thing a list of size 1.