Erlang: Questioning Programming Language Groupthink =================================================== .notes: plop ---- Failure is an not option ========================= ![Joe Armstrong](joerl.png) ---- What is? ======== A programming language * [Functional](p359-hudak.pdf) * Concurrency Oriented * Compiled * Fault-tolerant * Distributed named after: Agner Krarup Erlang, a Danish mathematician. ![Agner Erlang](Erlang.jpg) Why? === * The world is parallel * Write programs that run for ever. * "Let it crash" over Hope * Who understands threads? * Why care about multiprocessing? * Threads are definitely better than processes. * Mutlicore programming is voodoo. * Modify code when the app is still running Erlang makes some very hard things easy. ---- Peeking into the Erlangian Ant Colony ===================================== * Let them have processes! * Let processes do one(or few things) very well. * Let them talk to each other. ---- OO Sucks ======== -- [Joe Armstrong](http://www.sics.se/~joe/bluetail/vol1/v1_oo.html) ---- Anyway ... ========== Message Passing is Encapsulation Entities passing messages to each other do not know each other's innards. The message is the contract. "Either I understand what you are saying or I ignore you". (fail gracefully). See also: [Actor Model](http://en.wikipedia.org/wiki/Actor_model) ---- Shell ===== Play. !erlang >erl Erlang R15B (erts-5.9) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.9 (abort with ^G) 1> ---- Socket Programming, you say? ============================ [Holy beej.us!](http://beej.us/guide/bgnet/output/html/singlepage/bgnet.html#clientserver) * [Au Contraire ..](http://www.erlang.org/article/15) * [Borriing!!](http://jerith.za.net/writings/erlangsockettut.html) ---- Message Passing =============== Send !erlang Pid ! message Pid ! {do_this, make_me_a_sandwich} Receive !erlang receive PatternA [when Guard1] -> foo(); PatternB [when Guard2] -> bar_bar(); _ -> meh end. ---- Functional programming is weird =============================== now that's out of the way... ---- Functional Programming ====================== ![](fp4.png) Avoid state Avoid mutable data Eliminate Side Effects Recursion Small(er) functions Profit(?) ---- Punctuation! ============ * Commas (,) seperate arguments, data cons, patterns. * Periods (.) followed by whitespace seperate functions and expressions. * Semicolons (;) seperate *clauses*. eg: in if, try, catch receive expressions. !erlang %% Special Indiana Edition area({square, Side}) -> Side * Side; area({circle,Radius}) -> Pi = 3, Pi * Radius * Radius; area(_Heh) -> ok. [Bonus](http://www.addedbytes.com/blog/if-php-were-british/) ---- Algebra was a lie! ================= Variables are single assignment in Erlang. `=` is a pattern match * Variables *must* start with uppercase letter. No side effect $=>$ Parallelize programs No hanky panky with *shared memory*, locks. ---- Data types ========== * Integer * Float * Atom * Tuple * List ---- Strings? Nobody wants Strings! ============================== !erlang 1> [66,69,78,68,69,82]. "BENDER" Bit Syntax ---------- Writing wire protocols? that's a lost dark art! !erlang > Red = 2. > Green = 61. > Blue = 20. > Mem = <>. <<23,180>> Very compact and like, all binary and stuff. ---- Pattern Matching ================ Looks like assignment. It's not. !erlang X = 4. Bound for life! !erlang {X,Y} = {its, out_there} ---- Pattern Matching ================ Interesting !lua function greet(Gender,Name) if Gender == male then print("Hello, Mr. %s!", Name) else if Gender == female then print("Hello, Mrs. %s!", Name) else print("Hello, %s!", Name) end !erlang greet(male, Name) -> io:format("Hello, Mr. ~s!", [Name]); greet(female, Name) -> io:format("Hello, Mrs. ~s!", [Name]); greet(_, Name) -> io:format("Hello, ~s!", [Name]). ---- Guards ====== pattern matching cannot express things like range of values etc., !erlang driving_age(X) when X < 16; X > 99 -> true; driving_age(_) -> ---- Other Control Structures ======================== !erlang if Age > 16 -> drive(); Age > 18 -> vote(); end. Case !erlang case X off 1 -> foo(); 2 -> bar(); 3 -> baz() %% <- end. Data Strctures ============== * Tuples = `{apples, 10}` * Lists - `ShoppingList = [{apples, 10}, {oranges, 12}]` List Processing: !erlang [Head|Tail] = [66,69,78,68,69,82]. !erlang sum([H|T]) -> H + sum(T); sum([]) -> 0. List comprehensions: !erlang [cost(A)*B || {A,B} <- ShoppingList] ---- Functions ========= Functions: !erlang Z = fun(X) -> 2*X end. %% Anonymous too! A function by any other name ---------------------------- * Functions with same name * Same name and different arity * Function order! ---- Modules ======= An Erlang module !erlang -module(geometry). -export([area/1]). area({rectangle, Base, Height}) -> Base * Height; area({circle, Radius}) -> 3.14 * Radius * Radius; area({triangle, Base, Height}) -> area({rectangle, Base, Height})/2. %use geometry:area({rectangle, 4, 5}). geometry:area({circle, 4}). geometry:area({triangle, 4, 5}). ---- ok. === Pradeep Gowda @btbytes