Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to Erlang


Published on

Suitable for an audience with little exposure to functional programming. Concurrency and handling errors in Erlang are skipped due to time

Published in: Technology, News & Politics
  • Be the first to comment

  • Be the first to like this

Introduction to Erlang

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