Erlang is very addictive

Akshat Jiwan Sharma, Wed Aug 07 2013

A few days ago I stumbled upon an article by Joe Armstrong, what's all this fuss about Erlang. Most of the stuff written in the article is about concurrent processing. I liked what Joe wrote but having never done concurrent programming I could not appreciate it fully. One thing that stood out for me was in the last paragraph which I later learnt was called pattern matching, a common feature of some functional programming languages.

area({rectangle, Width, Ht}) -> Width * Ht;

area({square, X}) -> X * X;

area({circle, R}) -> 3.14159 * R * R.

It took me a while to recover from a state of dumbfounded disbelief that I was in after reading the piece of code. If you forget about all the symbols any one can read and understand what is happening here. Area is being calculated for three different figure types: a circle, a square and a rectangle. These few lines of code instantly sold me on Erlang. Could it really be that easy? I was still a bit skeptical but I summoned up some courage to learn more about it.

Learn you some Erlang proved to be very helpful. The author explains the concepts of Erlang in a fun way. After spending a few days with Erlang, I have got to say that it is one of the most rewarding programming languages that I have come across.

Just for fun I re-visited some parts of the code that I wrote for specter and translated it in Erlang. For instance here is a piece of code that checks for the user role.

var contibutor = admin.length>0 ? setContibutorDetails("admin",admin[0]) : 
                         user.length>0? setContibutorDetails("user",user[0]):
                          guest.length>0?setContibutorDetails("guest",guest[0]):false;

In Erlang it could be done like so.

contributor(admin,details)-> setContributorDetails(details);
contributor(user,details)-> setContributorDetails(details);
contributor(guest,details)-> setContributorDetails(details).

which looks a lot simpler. Learning a new language can be a daunting task. Especially if the language is the one that challenges the way you write programs. Erlang is one such language. I can hardly call it radical considering the it has been out for almost two decades but it is still radical to me. Already I have started approaching the problems in a different way. The programs that I write seem more readable to me and frankly I have warmed up to Erlang.

I have also started learning about concurrent programming. While most of this stuff is arcane knowledge in other programming languages, Erlang introduces it pretty early since it is a fundamental part of the language. Lately I have started to use message passing to solve problems in clever ways. I am glad I took a dive into the wonderful world of eralng and reader if you are inclined towards programming you should check it out some time. Maybe you will like it.


comments powered by Disqus