Erlang: Questioning Programming Language Groupthink

plop

Presenter Notes

Failure is an not option

Joe Armstrong

Presenter Notes

What is?

A programming language

  • Functional
  • Concurrency Oriented
  • Compiled
  • Fault-tolerant
  • Distributed

named after: Agner Krarup Erlang, a Danish mathematician.

Agner Erlang

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.

Presenter Notes

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.

Presenter Notes

OO Sucks

-- Joe Armstrong

Presenter Notes

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

Presenter Notes

Shell

Play.

1 >erl
2 Erlang R15B (erts-5.9) [source] [64-bit] [smp:2:2] [async-threads:0] [hipe] [kernel-poll:false]
3 Eshell V5.9  (abort with ^G)
4 1>

Presenter Notes

Socket Programming, you say?

Holy beej.us!

Presenter Notes

Message Passing

Send

1 Pid ! message
2
3 Pid ! {do_this, make_me_a_sandwich}

Receive

 1 receive
 2   PatternA [when Guard1] ->
 3     foo();
 4   PatternB [when Guard2] ->
 5     bar_bar();
 6
 7   _ ->
 8   meh
 9
10 end.

Presenter Notes

Functional programming is weird

now that's out of the way...

Presenter Notes

Functional Programming

Avoid state

Avoid mutable data

Eliminate Side Effects

Recursion

Small(er) functions

Profit(?)

Presenter Notes

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

Presenter Notes

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.

Presenter Notes

Data types

  • Integer
  • Float
  • Atom
  • Tuple
  • List

Presenter Notes

Strings? Nobody wants Strings!

1 1> [66,69,78,68,69,82].
2 "BENDER"

Bit Syntax

Writing wire protocols?

that's a lost dark art!

1 > Red = 2.
2 > Green = 61.
3 > Blue = 20.
4 > Mem = <<Red:5, Green:6, Blue:5>>.
5 <<23,180>>

Very compact and like, all binary and stuff.

Presenter Notes

Pattern Matching

Looks like assignment. It's not.

1 X = 4.

Bound for life!

1 {X,Y} = {its, out_there}

Presenter Notes

Pattern Matching

Interesting

 1 function greet(Gender,Name)
 2   if Gender == male then
 3     print("Hello, Mr. %s!", Name)
 4   else if Gender == female then
 5     print("Hello, Mrs. %s!", Name)
 6   else
 7     print("Hello, %s!", Name)
 8 end
 9
10 !erlang
11 greet(male, Name) ->
12   io:format("Hello, Mr. ~s!", [Name]);
13 greet(female, Name) ->
14   io:format("Hello, Mrs. ~s!", [Name]);
15 greet(_, Name) ->
16   io:format("Hello, ~s!", [Name]).

Presenter Notes

Guards

pattern matching cannot express things like range of values etc.,

1 driving_age(X) when X < 16; X > 99 ->
2 true;
3 driving_age(_) ->

Presenter Notes

Other Control Structures

1 if
2     Age > 16 ->
3         drive();
4     Age > 18 ->
5         vote();
6 end.

Case

1 case X off
2     1 ->
3         foo();
4     2 ->
5         bar();
6     3 ->
7         baz() %% <-
8 end.

Data Strctures

  • Tuples = {apples, 10}
  • Lists - ShoppingList = [{apples, 10}, {oranges, 12}]

List Processing:

1 [Head|Tail] = [66,69,78,68,69,82].
2
3 !erlang
4 sum([H|T]) -> H + sum(T);
5 sum([]) -> 0.

List comprehensions:

1 [cost(A)*B || {A,B} <- ShoppingList]

Presenter Notes

Functions

Functions:

1 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!

Presenter Notes

Modules

An Erlang module

 1 -module(geometry).
 2 -export([area/1]).
 3
 4 area({rectangle, Base, Height}) -> Base * Height;
 5 area({circle, Radius}) -> 3.14 * Radius * Radius;
 6 area({triangle, Base, Height}) -> area({rectangle, Base, Height})/2.
 7
 8 %use
 9
10 geometry:area({rectangle, 4, 5}).
11 geometry:area({circle, 4}).
12 geometry:area({triangle, 4, 5}).

Presenter Notes

ok.

Pradeep Gowda

@btbytes

Presenter Notes