Erlang Oddities by Brujo Benavides

October 26, 2021

In the Erlang Oddities presentation by Brujo Benavides, he shares unconventional ways of solving programming exercises and unexpected behaviors in Erlang. He discusses an anecdote about an interview exercise called FizzBuzz, where he aims to write a script in the most unconventional way possible. Brujo also shares an unexpected behavior when using parentheses in module definitions and advises using the short circuit operator to fix comparison operator issues. The speaker then discusses unconventional ways of implementing the FizzBuzz problem in Erlang and handling exceptions using the try catch mechanism and an inelegant method. Brujo also shares his experiences with handling errors in Erlang and the confusion around passing arguments to Erlang’s version server. All examples shared in the presentation are from real-life experiences.

Erlang Oddities: A comprehensive overview

Brujo Benavides presented an intriguing talk titled Erlang Oddities. As a seasoned Erlang developer and blogger, Brujo shared some unconventional and lesser-known aspects of Erlang, providing a mix of practical insights and humorous anecdotes.


Introduction and the FizzBuzz anecdote

Brujo kicked off the presentation with a lighthearted anecdote about the classic “FizzBuzz” interview exercise. This simple coding challenge involves writing a script to print numbers below a given integer, with specific outputs for numbers divisible by three, five, or both. However, Brujo aimed to solve this exercise in the most unconventional way possible. He emphasized that the script must be runnable from the command line, a detail often overlooked, and injected humor by referencing a comedy presentation by Gary Bernhardt.


Unexpected behavior with parentheses and module definitions

Moving into the quirks of Erlang, Brujo highlighted an unexpected behavior when using parentheses in module definitions. He explained how a line ending with a colon could form a complete Erlang module even without functions. This was followed by a demonstration of a function called “up_to” that prints numbers up to a given number. Brujo encountered an issue with comparison operators in guard clauses having different precedences, leading to unexpected results. He advised using short-circuit operators to fix such issues.


Tackling FizzBuzz in Erlang

Brujo then discussed an unconventional approach to implementing the FizzBuzz problem in Erlang. He pointed out that Erlang’s if expressions require an else clause to evaluate properly, which can make the code look clunky. Despite this, he compiled and ran the code, which successfully printed numbers up to 10, though not yet producing the desired FizzBuzz output. He mentioned that better solutions, such as using pattern matching or case expressions, would be covered later.


Exploring Else Ifs and list comprehensions

The presentation took a deeper dive into writing Erlang code in unusual ways, focusing on else ifs and list comprehensions. Brujo showed an example where an else if was turned into a short-circuiting boolean expression. He also demonstrated how list comprehensions can simplify complex code into one-liners. This method avoids executing unnecessary code when conditions aren’t met, showcasing Erlang’s powerful and flexible syntax.


Handling unexpected behaviors in list comprehensions

Brujo shared an unexpected behavior in Erlang list comprehensions when filter conditions are neither true nor false. Instead of failing, the compiler returns an error about a wrong generator or filter. He suggested using a catch block to suppress errors, though this approach can hide potential issues. This discussion underscored the importance of understanding Erlang’s intricacies to write robust code.


Implementing FizzBuzz with OTP and GenServer

Next, Brujo tackled the FizzBuzz algorithm using the OTP framework and GenServer. He warned against the maintenance issues of using copied code from the web and demonstrated a simple, though inefficient, implementation. He explained that creating a new server process each time the function runs can lead to memory leaks. The importance of using parentheses with the module macro was also discussed, along with handling improper inputs.


Managing exceptions and exit signals

The talk then covered handling exceptions in Erlang. Brujo initially encountered an unexpected exception and suggested using the try catch mechanism, though he found it unsatisfactory. Instead, he introduced an inelegant method using a catch statement to swallow exceptions. He also discussed trapping exit signals to manage process restarts, explaining that not linking processes and allowing the GenServer to die can keep the server running without linking processes.


Clarifying arguments in version server

Brujo addressed the confusion around passing arguments to Erlang’s version server, advising against using lists and recommending tuples or maps with named arguments. This section highlighted the importance of understanding documentation nuances and choosing the right data structures for passing arguments.


Error handling and Erlang’s dual nature

The discussion then focused on Erlang’s approach to error handling. Unlike other languages, Erlang uses the concept of returning an error as the expected result, allowing functions to continue executing. This approach can be confusing due to Erlang’s dual nature: sequential and concurrent. Brujo explained using the throw function for non-local returns and provided an example of stopping a function loop when encountering a non-number element.


Concluding thoughts and audience interaction

Towards the end, Brujo shared the inspiration behind collecting these Erlang oddities: a tricky debugging experience. He began collecting more oddities from colleagues and started documenting them. The examples presented were all from real-life experiences, offering valuable insights for the audience.


Wrapping up

Brujo Benavides’ presentation on Erlang Oddities provided a captivating look into the unconventional aspects of Erlang. From quirky behaviors to practical advice on handling exceptions and arguments, the talk was both educational and entertaining. It highlighted the depth and complexity of Erlang, making it clear that understanding these oddities can lead to more effective and efficient coding practices.


Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.