PPL30 Day 10: Erlang

In what felt like a departure from the norm, I fell off the JVM today and took a look at Erlang. Erlang is a functional language originally designed to handle concurrency easily while doing telephony work. It, like old-school telephony guys, is a bit odd.

This is part of the Peer Pressure Learning 30 series. To learn more about what in the hell that means, see the intro to the series, A Good Time To Learn.

Installation

brew install erlang

erlc -fanc -y --non-ex /istent/com/mand

Okay. I’m lying. Just brew install erlang.

Resources

Learn You Some Erlang for Great Good

You read that right. I mostly just read, and sometimes quickly skimmed, an online book called Learn You Some Erlang for Great Good. It is modeled (with permission) on Learn You a Haskell for Great Good, which I’ll see sometime in the next few weeks, and is inspired by Why The Lucky Stiff’s Why’s Poignant Guide to Ruby.

Honestly, Learn You Some Erlang, or LYSE, is pretty great. Well, the first half is, anyway. From about the Types chapter on (5th of 13), I had to skim – actually, speed-read without any speed-reading training. I do think I learned a lot (prior to the commencement of speed reading), and I enjoyed it (even the speed reading).

What I Think I Now Know

I feel as if I understand a lot more about functional programming in general, actually. Perhaps it’s because I’ve been through Scala and Clojure recently as well, but some of the principles, such as, of course, avoiding mutable data (and the methods used to achieve it), make more sense. Also, pattern matching! AHA! Whenever JR mentions pattern matching during a Scala conversation, I’d just chalk it up as another thing about Scala that I don’t yet get. (This was a bit disturbing for me, because pattern matching sounds like something obvious and simple. Shouldn’t I know this? Sometimes I’m very aware of my complete lack of a CS foundation.)

So, when JR wrote this Scala FizzBuzz:

<% coderay(:lang => :scala, :line_numbers => :inline) do #_ -%> 1 to 100 map { x => (x % 3, x % 5) match { case (0, 0) => “fizzbuzz” case (0, ) => “fizz” case (, 0) => “buzz” case _ => x.toString } } foreach println <% end -%>

… I thought it looked like pseudo-code with winking owl faces mixed in. (Owl face: (0,0). Winking owl face: (0,_)). Well, now I’ve been inspired to do something similar in Erlang using pattern matching and function clauses:

<% coderay(:lang => :erlang, :line_numbers => :inline) do #_ -%> -module(fizzbuzz). -export([fizz/3, doit/0]).

fizz(0, 0, Num) -> io:format(“FizzBuzz~n”); fizz(0, , Num) -> io:format(“Fizz~n”); fizz(, 0, Num) -> io:format(“Buzz~n”); fizz(_, _, Num) -> io:format(“~B~n”, [Num]).

doit() -> Go = fun(El) -> fizz(El rem 3, El rem 5, El) end, L = lists:seq(1,100), lists:foreach(Go, L). <% end -%>

Basically, in fizz(), we’re expecting the arguments to match a pattern. In this case, if the first and second arguments are both 0, print ‘FizzBuzz’ (~n is a newline character). In the next lines starting with fizz, if the first argument is 0, and the second argument is anything but 0, print out ‘Fizz’. Next, if the first argument is anything but 0, and the second argument is 0, print ‘Buzz’. Finally, if both the first two arguments are something other than 0, print out the number (via Erlang’s weird I-wish-we-just-had-sprintf control sequence crap).

I’m sure this is crappy Erlang code, by the way. Mostly, I believe there has to be a way to make this more succinct. However, time is not immutable.

I’m going to have to circle around and finish LYSE. It really was an enjoyable read – easier to parse than I even remember Why’s guide being (so, a lot fewer cartoon fox diversions and the like – which could be a negative, I suppose). Erlang is an intriguing language, but, though I admittedly know little about it, I cannot at this time imagine using it for anything other than a fun diversion and/or brain exercise.


What better time than now for trying Haskell? While I’ve got at least a tenuous, sweaty grasp on the railing of the functional programming tilt-a-whirl bucket seat, I might as well give it a shot. Plus, I’ll be able to directly compare Learn You a Haskell for Great Good to Learn You Some Erlang for Great Good. I hope it turns out to be great, and good.

Don’t forget to checkout Tim’s Day 10 session with Clean Code!