2. Erlang? Small language designed more than 20 years ago by Joe Armstrong, Robert Virding and Mike Williams of Ericsson’s CS Lab. Functional Language. Lightweight concurrency model (aka Actors). Yes, Scalahas a Actors model where they share similar syntax and machinery. Great for soft real-time systems. E.g. Second Life’s chat service Not good for number crunching. There are better tools for that e.g. SciPyNumPy
3. Yah yah yah…WHO actually uses Erlang? Amazon uses Erlang to implement SimpleDB as part of its EC2 services Facebook uses Erlang to power its backend chat service for its 100 million active users Ericsson uses Erlang to support its GPRS and 3G mobile networks T-Mobile uses Erlang in its SMS and authentication systems
4. So, what are the benefits of Erlang? Easy to learn, easy to use. Expressing distributing computing is easier Garbage collection is implemented on a per process basis An process in Erlang is light-weight i.e. not a OS process. Each pair of processes communicate by passing messages Designed to operate behind firewalls. Think private compute cloud
5. Let’s learn how Erlang does it Numbers Variables (they’re not variable) Atoms Boolean Algebra and Comparison Operators Tuples Lists (and comprehensions) Modules in Erlang
6. There’s more… High-order functions and Lambdas Errors and catching them (We don’t have time for this) Concurrency in Erlang and Actors etc (We don’t have time for this too) Finally we’ll end with a demonstration
7. Working with Erlang In FP, statements do not exist only expressions. Expressions always return a result and more often than not, have no side effects at all; statements (depending on the lang) may / may not return a result but always have side effects. Expressions, in Erlang, have to be terminated with a period followed by whitespace (,) E.g expression involving numbers 2 + 5. 5 / 2. (same as 5 div 2.) 5 rem 2. -50 * (100 – 499). (Erlang understands the typical math operator precedences) 2#101 (same as the decimal number 5 only expressed in binary i.e. base 2) 16#cafebabe (do you know what this magic number is expressed in base 16?)
8. Variables in Erlang (seriously???) No such thing as a variable variable In Erlang, creating a variable and associating with a value is termed bounding. You cannot change the value once bounded. One = 1. One = 2. (Will not succeed) Two = 2. Two = One + One. (Will succeed) _ = 999999999 (Will always succeed. ‘_’ is the i-don’t-care-variable) The ‘=‘ acts as an assignment and pattern matching operator. Pattern matching occurs with/without the ‘=‘. More later.
9. Atoms (Like the physics term but not like it) In physics of the 1920s, atoms were the smallest things known to man. Today, there are units smaller than the atom like hadrons, quarks. In Erlang, an atom is a literal. hello. (Typing Hello. means to extract the value of Hello) ‘Hello there’. (anything in quotes is an atom) ‘_this_is_a_#really_!long_@string’.
10. Boolean Algebra, Operators In Erlang, the atom true is Truth whereas false is Falsehood. You can use =:= and =/= for comparing equality and inquality You can use == and /= for comparing equality and inequality if you don’t care about exactness / precisions 5 =:= 5. (will return true) 5 == 5. (will return true) 5 =:= 5.0. (will return false) 5 == 5.0. (will return true)
11. Tuples They allow you to aggregate values to form some sort of data structure. X = 10, Y = 4. Point = {X, Y}. (Tuple is created) {MyX, _} = Point. (Pattern matching at play. MyX will get the value 10) {_, _, _} = Point. (will fail and erlang will complain RHS don’t match LHS)
12. Lists In FP, lists are completely indispensable. Same here You can create lists, obviously. You can concat, intersect them. You can retrieve the head or tail of a list via ‘hd’ and ‘tl’ commands in the ‘erlang’ module MyList = [1,2,3,4,5]. MyList ++ [7,8] - - [1,2,3,4,5,6,78]. (equivalent to (MyList ++ ([7,8] - - [1,2,3,4,5,6,7,8])) ) hd( [1,2] ). (will return 1 and take note its not a list) tl([1,2]). (will return [2] which is a list)
13. List Comprehension In other languages, its called a for-comprehension. Let’s look at code examples [2 * N | | N <- [1,2,3]]. (returns [2,4,6]) [X | | X <- [1,2,3,4,5,6], X rem 2 =:= 0]. (returns [2,4,6] ) Let’s look at a more complex example (taken from sg.finance.yahoo.com) [ {Quote,Price} | | {Quote, Price} <- [ {e3s, 0.4}, {g13.si, 1.66}, {'5im.si',0.22}, {e5h.si, 0.64} ], Price > 0.5]. (returns [{'g13.si',1.66},{'e5h.si',0.64}]) Nested-for-loops (Ugly word in Erlang, but u get the meaning) [ {X, Y} | | X <- [1,2], Y <- [3,4]]. (returns [{1,3}, {1,4}, {2,3}, {2,4}])
14. How to avoid clustering all code in a single file? You need modules. You need to declare your module’s name, attributes, what functions are exported
15. Compiling and using your modules There are a couple of methods to do this but for the sake of brevity, here’s the simplest
16. Functions and Lambdas Functions are easy to write in Erlang. You don’t need to say ‘def’, ‘function’ or give any access specifier like ‘public’, ‘protected’ any of that nonsense. Here’s an example of a Mapper function. map(_, []) -> []; map(F, [H|T]) -> [F(H) | map(F, T)]. Another example of a Folder function fold(F, Start, []) -> Start; fold(F, Start, [H|T]) -> fold(F, F(H, Start), T). A lambda function is simply a nameless function and sometimes called an anonymous function. They can do pretty much anything except recursion. The form is like this: fun (Args1) -> Expression1, … , Expression N ; (Args2) -> Expression1, … , Expression N; end.