plop
A programming language
named after: Agner Krarup Erlang, a Danish mathematician.
Erlang makes some very hard things easy.
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
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>
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.
now that's out of the way...
Avoid state
Avoid mutable data
Eliminate Side Effects
Recursion
Small(er) functions
Profit(?)
!erlang %% Special Indiana Edition
area({square, Side}) -> Side * Side;
area({circle,Radius}) -> Pi = 3, Pi * Radius * Radius;
area(_Heh) -> ok.
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.
1 1> [66,69,78,68,69,82].
2 "BENDER"
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.
Looks like assignment. It's not.
1 X = 4.
Bound for life!
1 {X,Y} = {its, out_there}
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]).
pattern matching cannot express things like range of values etc.,
1 driving_age(X) when X < 16; X > 99 ->
2 true;
3 driving_age(_) ->
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.
{apples, 10}
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]
Functions:
1 Z = fun(X) -> 2*X end. %% Anonymous too!
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}).
Pradeep Gowda
@btbytes
Table of Contents | t |
---|---|
Exposé | ESC |
Full screen slides | e |
Presenter View | p |
Source Files | s |
Slide Numbers | n |
Toggle screen blanking | b |
Show/hide slide context | c |
Notes | 2 |
Help | h |