Not sure if I have to repeat some of the definitions given to this language, because I doubt that my readers read only this blog and never heard about Erlang. But who would start searching for “Erlang” if you are already here, so to avoid this dilemma I just copied one of its definitions later on.
Erlang is great and hard language at the same time. Its syntax easily looks somewhat alien to many developers, not saying about normal people, who are capable of reading only few human languages. On the other hand ideas behind Erlang are just astonishing. “Let it crash” concept works, because there is no shared state between processes, and if something crashes you just restart stuff, not worrying about overhead as processes are lightweight. And how does this sound to you: dynamic language in which you can change code on the fly? I don’t think you can allow for downtime when you are in a rocket. Not sure if NASA uses Erlang, but many telecom companies use it for sure, which resulted in birth of Open Telecom Platform (OTP) – a library for telecom applications.
I’ve found following explanation very sensible:
Erlang was developed at Ericsson and was designed from the ground up for writing scalable, fault-tolerant, distributed, non-stop, soft-realtime applications. Everything in the language, runtime and libraries reflects that purpose, which makes Erlang the best platform for developing this kind of software.
Use Erlang if you want your application to:
- handle very large number of concurrent activities
- be easily distributable over a network of computers
- be fault-tolerant to both software & hardware errors
- scale with the number of machines on the network
- be upgradable & reconfigurable without having to stop & restart
- be responsive to users within certain strict timeframes
- stay in continuous operation for many years
Please visit http://veldstra.org/whyerlang/ for more “why Erlang?”.
This or some similar question often is very interesting when its about some exotic or small language. Hard to answer, but I’ve found this really nice site: http://roberto-aloi.com/languagesintheworld/#. Try it out to figure out who is coding Erlang/Haskell/Scala/… in your country.
In my Haskell post I wrote “I’m too new to Haskell to talk about some advanced features and to write programs which are over 10 lines of code.” I can ensure that now I can write Erlang programs which are more than 10 lines of code, but I won’t be able to confidently talk about most of aspects of this language either.
I can write larger programs, because I wrote one. Pitfall here is that I don’t completely understand how it works. I believe this is due to the fact, that I’ve chosen wrong problem to solve with this language. Erland is not really build for stuff like “run this for 10 seconds”, but rather for stuff like “that’s your message, deal with it”. Though, I could easily be wrong.
I decided that my first Erlang program will be “sleeping barber” problem, which I’ve already done in Clojure (BTW, there is small bug, spot it!). Once again:
Problem called "sleeping barber." was created by Edsger Dijkstra in 1965. It has these characteristics:
- A barber shop takes customers.
- Customers arrive at random intervals, from 10 to 30 milliseconds.
- The barber shop has three chairs in the waiting room.
- The barber shop has one barber and one barber chair.
- When the barber’s chair is empty, a customer sits in the chair, wakes up the barber, and gets a haircut.
- If the chairs are occupied, all new customers will turn away.
- Haircuts take 20 milliseconds.
- After a customer receives a haircut, he gets up and leaves.
Write a multithreaded program to determine how many haircuts a barber can give in 10 seconds.
Counter – does nothing smart except of counting number of customers with new haircut
Crazy stuff. For example, take a look at how I implemented continuous generation of customers. It is tail recursion function, which takes 10000 as starting WorkTime and generates customer after 10-30ms of thread sleeping, then calls itself with WorkTime minus time needed to send customer to barbershop. It continues until function is out of WorkTime. Similar I do for barber_work_day.
Here I just initialize random, create processes and kick-off generation of customers and barber’s work day.
Un-obviously it executes slightly longer than 10 seconds (12-15), so when I call counter:get_count(Counter) exactly after 10 seconds I’m getting slightly lower numbers than expected, so I just call get_count in console until number stops growing with some very realistic number, like 434.
Now you can blame me for my dumbness, because it is really a lot of code for quite simple problem and it took me good 6+ hours to implement.
I really hope you liked this post! If true, instead of blaming me, you are welcome to share your own experience with Erlang.