The Hyperpessimist

The grandest failure.

Seven Languages, Week 5: Erlang

So, Erlang this week. Well, I have to admit that I understand why people think Erlang is wonky. It seems like they took the odd syntax of a logical programming language (hint, hint, hint) and made a general purpose language. Oh wait, they did.

I have to admit that I kinda liked the concepts, the pattern matching on tuples, the restarting of processes. This all feels simple and powerful. And to my surprise the BEAM VM does not use that much memory as I was expecting. Things were instataneous on my old trusty ThinkPad. Much appreciated.

Due to tail call optimization, loops with pattern matching are actually pretty elegant, you just call the function again and that’s it. The syntax for transmitting messages is straightforward, no wonder Scala was inspired by this.

But there’s a number of strange things: the exact syntax of delimiters is driving me up the wall. Like in Prolog you can define a predicate with pattern matching, but in Erlang definitions of a function must be consecutive and be separated by semicolons and finished with a full stop. This example:

1
2
3
4
5
words("") -> 0;
words([32]) -> 0;
words([_]) -> 1;
words([32|Tail]) -> 1 + words(Tail);
words([_|Tail]) -> words(Tail).

Every time I had syntax errors in Erlang, these were caused because the interpunctuation was wrong. Because it is non-intuitive and random. Oh well. From the example, you can see another wonky thing: when you put numbers in a list, Erlang sees this as a string. So this example calculates the number of words in a string separated by spaces (ASCII 32). It is a kinda odd way.

The book chapter on Erlang was quite ok I’d say, just day 3 homework was so frustrating that I left it out. Let me tell you how the tasks looked like:

  • Implement a task that monitors another and restarts it if it dies
  • Implement a task that monitors itself and restarts itself if it dies
  • Implement a monitor that monitors the monitor and restarts itself if it dies

Well great, basically the same thing three times, so I just left out this homework. The remaining tasks were surprisingly fun.

Back to Erlang: I can’t see myself using Erlang because of these syntactic oddities, but I liked the concepts. Maybe something like Lisp Flavored Erlang or perhaps better Elixir would fit my needs better. Actually the latter seems to be in the spirit of CoffeeScript to Erlang, so I’d probably try this first.