Erlang

advertisement
Erlang
I can Erlang and you can too!
William Holt
CSC 415, Fall 2013
Dr. Bill Lyle
Table of Contents
Erlang .............................................................................................................................. 1
I can Erlang and you can too! ........................................................................................ 1
Who was Erlang? ................................................................................................................................... 1
Erlang Origins ....................................................................................................................................... 1
Erlang’s Design, Syntax and Schematics ........................................................................ 3
Evaluation .................................................................................................................... 7
Erlang
ii
William Holt
Erlang
I can Erlang and you can too!
Who was Erlang?
It may seem like a strange question to ask about a
programming language but Erlang’s namesake, Agners
Erlang, was a Danish mathematician who founded the
Suspendisse potenti.
areas of traffic engineering and queuing theory with his two publications on the theory of
telephone traffic, “The Theory of Probabilities and Telephone Conversations”, which was
published in 1909 and proved that Poisson Distribution applies to random telephone traffic.
His 1917 publication, “ Solution of some Problems in the Theory of Probabilities of
Significance in Automatic Telephone Exchanges”, contained formula for calculating drop
rate and call wait time. So why even bring this up? Well, as it turns out…
Erlang Origins
Ericsson, a significant player in the telephony industry (see where I’m going here?) wanted
to develop something to help them with the development of telephony applications. In the
mid-1980s, Joe Armstrong, Robert Virding, and Mike Williams began work on Erlang. The
Erlang
1
very first versions of Erlang were implemented in Prolog and drew a lot of influence from
PLEX (Programming Language for Exchanges), Ericsson’s previous language. According to
language designer, Joe Armstrong, Erlang was hoisted “from the lab into the real world”
when Ericsson’s AXE exchange, a product that controlled digital telephone exchanges
crashed. The team at Ericsson designed Erlang to be a very resilient language, with fault
tolerance, support of distributed systems, soft real-time and non-stop applications that could
be edited on the fly without stopping the system.
After the crash, Ericsson released a new exchange switch contained over a million lines (!)
of Erlang and achieved a reliability rating of nine 9’s. The nine scale of reliability begins
with one nine or 90% uptime and ends at seven nines or a 99.99999% uptime. So this means
that the new Erlang powered service had an uptime of 99.9999999% and a downtime of
0.00315 seconds per year. For whatever reason, even with this, Ericsson had a change of
heart on using proprietary languages and opted for non-proprietary ones and Erlang was
banned from new products. Erlang was open-sourced soon after and the ban was lifted in
2004. Since then, native symmetric multiprocessing support has been added to the language.
Erlang
2
Erlang’s Design, Syntax and Schematics
Data Types
Erlang is different from languages such as C++ in that you can only assign something to a
variable once. Though it makes up for this with pattern matching.
Atoms
Aside from the standard integer and float data types and operators, Erlang uses several other
data types such as Atoms. Atoms are can be related to a static final veriable like in java, but
unlike C++, they remain in the object code. The only thing you can do with atoms are
compare them against patterns.
Examples of atoms are:
abc.
‘I want pizza’
Strings
The next type of note is strings, there isn’t a string data type in Erlang, though they are
represented as lists of ascii values and represented by “ “. An example of this would be:
"Hello World"
[72,101,108,108,111,32,87,111,114,108,100]
Erlang
3
One of the biggest differences between atoms and strings are that you can actually manipulate
strings where as you cannot atoms.
Lists and tuples are pretty similar, as they can mix types freely but lists are used for a variable
number of entries, while tuples can only be used for a fixed amount. How do you tell what’s
what? Lists are denoted by [], while tuples are denoted by {}. Lists and tuples are compared
differently too. Where lists are compared lexicographically while tuples are only compared by
the number of elements.
More complex data structures usually live within nested tuples and lists, an example of such is
below.
[{person,"Mike","Williams", [ {shoeSize,41},
{likes,[boats,wine]}] }
]
This is a list of tuples of type person, first name, last name and a bunch of attributes.
Lists and what you can do with them
You can do all sorts of things with lists in Erlang, it makes such things as finding the max,
sum and sorting a list super easy.
With a few commands you can do lines of code in other languages and just one!
lists:max([1,2,3]).
3
lists:sum([3,4,10,7,9]).
Erlang
4
33
lists:sort([2,1,3]).
[1,2,3]
Variables
As I stated before, variables are only single-assigned, so you cannot change them once you
have assigned a value to them. Another thing that is imperative is that all variables begin
with a capital letter and only contain letters, numbers or underscores. For example
something like this:
Apples = 1.
Apples = Apples * Apples.
Would not work, where as:
Apples = 1.
BunchApples = Apples * Apples.
would.
Also, contrary to C++ and other languages, global variables don’t exist in the world of
Erlang, which makes debugging a bit easier!
Functions are fun!
All functions (funs) in Erlang are contained inside Erlang modules, you cannot run them
outside in the shell. They tie all of the data types together so you can actually do
Erlang
5
complicated things, like find the area of a circle.
area({square, Side}) -> Side * Side;
area({circle, Radius}) -> math:pi() * Radius * Radius.
You would call this by using area({circle, 3}). However, with pattern matching, calling area
will actually go through all of the functions called area and find one that matches the one
you called before returning it.
Erlang
6
Evaluation
Erlang is designed for applications that are mission critical and must be up at all times.
Aside from telephone switches, Erlang powers quite a few things today. Including
Facebook’s messaging service, which has over 100 million users, as well as Whatsapp, a
very popular messaging app with over 2 million users per server. While messaging is
Erlang’s bread and butter, it is used in high-frequency trading programs and well as many
distributed database projects, a major one being CouchDB.
Erlang processes execute in their own memory space, rather than shared. In addition to this,
processes have their own heap and stack, so that each process cannot interfere with each
other, which avoids nasty concurrency issues such as deadlocks.
Processes pass messages to each other asynchronously and chosen selectively so they are not
necessarily executed in the order in which they are received which makes concurrency
robust. This makes the process of making multi-core friendly applications very easy in that
all of the work is done for you. You don’t need to change anything in your program!
Programs also take less time to write since you do not really need to worry about fault
tolerance, memory management or communications at a high level, since all of this is built
into the language and VM. This often translates to a sizable performance boost over C++.
Readability and writability for Erlang is a bit daunting at first, I know it was for me when
we got into the more hairy stuff at ErlangCamp. However, if you can take the time to learn
it you can reap some big rewards. Erlang’s ability to run concurrent applications on nearly
Erlang
7
anything with little to no downtime is music to anyone’s ears that need a system or
application that doesn’t need to go down … ever. However, even though it is growing in
popularity, you can be hard pressed to find someone to actually works in Erlang 24/7, so
the cost of learning it and getting a decent mentor could be high, but again, the cost reward
ratio can be pretty high.
Now go out there and learn some Erlang!
Erlang
8
Bibliography
Francesco Cesarini and Simon Thompson, Programming Erlang
http://erlang.org
http://learnyousomeerlang.com/
http://plus.maths.org/content/os/issue2/erlang/index
Erlang
9
Download