Introduction to Erlang
Erlang is a functional, concurrency-oriented,
distributive, fault-tolerant programming
language.
1
Philosophy of Erlang
Erlang was designed for writing concurrent programs
that "run forever". - Joe Armstrong
Let it fail. Have another process deal with it.
Fault tolerance requires at least *two* computers.
2
Erlang Mindset
Lots of little tasks
Tons of processes
Each process does a task very well
Ant Colony
Ants are processes sending messages to each other
3
Why Erlang?
4
Erlang makes hard things
easier.
5
Concurrency
6
7
Spawning Processes
8
F = fun() ->
io:format(hi)
end,
spawn(F).
>> hi<0.34.0>
9
spawn(io, format, [hi]).
10
Message Passing
11
12
Pid ! Message
Pid ! {do_task, run_home}
Pid ! 2
13
receive ... end
receive
Pattern1 [when Guard1] ->
Expressions1;
Pattern2 [when Guard2] ->
Expressions2
end.
14
Messages are contained in a processs
inbox.
receive
_ ->
ok
end.
15
Fault Tolerance
16
17
18
which is why erlang was
designed to be....
19
Distributive
20
21
Pid ! Message
where Pid exists on another node.
22
Erlang Nodes
Can only talk if the erlang cookie is the same
Can exist on the same machine or different machines
Does have overhead, isnt free
Can have C nodes (+ other node types that speak
erlang)
Communicate via Global Name Registry, Specific Pids,
or by Node name
23
The Basics
24
Erlang is Functional
25
Periods, Commas,
Semicolons Oh My!
People think Erlangs syntax is ugly. Its actually
very logical.
26
Periods ( . ) end everything except when
Semicolons ( ; ) end clauses
and Commas ( , ) separate expressions.
27
calculate_area({square, Size}) ->
Size * Size;
calculate_area({circle, Radius}) ->
Pi = 3.14,
Pi * Radius * Radius;
calculate_area(_Unknown) ->
ok.
28
case Expression of
{do_task, run_home} ->
Home = 123,
spawn(run, home, [Home]);
_ ->
ok
end.
29
Data Types
30
Integer - however big you can imagine
Floats
Atoms - similar to a ruby symbol, they are used to
represent non-numerical constants (true, false, ok,
error)
Tuple - Fixed length collection of items ({one, two,
three} or {one, two} etc)
Lists - Stores a variable number of things ([1, 2, {one,
two}, $a])
31
Missing Links...
32
Strings - A string is just a list of integers in erlang.
Booleans - Use atoms (true, false)
Hash - Property List ( [tuple()] ) shown later
33
Pattern Matching
Once you pattern match, variable assigning feels dirty.
34
1 = 1 ( ok )
1 = 2 ( error thrown bad_match )
Value = {2, 4},
{Width, Height} = {2, 4} ( ok )
[Head|Tail] = [1,2,3,4] ( ok )
35
Variable Binding
or restricting infinite possibilities.
36
An unbound variable can be anything it
needs to be (infinite possibilities)
A.
37
Pattern matching an unbound variable is
like restricting what it can be.
A = 1.
A = 2. (error bad_match)
38
Tuples
Single entity that holds a collection of items.
39
{1, 2, 3}.
{one, two}.
{user_id, 2, user_name, asceth}.
40
Lists
Variable sized container of items.
41
List = [1, 2, 3],
NewList = List ++ [4, 5, 6].
>> [1, 2, 3, 4, 5, 6]
[Head|Tail] = NewList.
>> Head -> 1
>> Tail -> [2, 3, 4, 5, 6]
42
[tuple()]
Property Lists or semi-hashes.
43
PList = [{key, value}, {key1, value1}].
case lists:keysearch(key, 1, PList) of
false ->
error;
{value, {Key, Value}} ->
Value
end.
44
keydelete - deletes tuple with specified key in list of
tuples and returns new list
keymap - returns list of tuples where Nth element is
replaced be specified fun
keymember - tests for existence of key
keymerge - combines two tuples with tuple1 taking
priority
ukeymerge, ukeysort - like keymerge but removes
duplicates in tuple2
45
Functions
Pattern matching, Function Arities & more
46
Functions with the same name can exist
Functions with the same name and different arity can
exist
Erlang tells you if a function cannot be called due to
order of definement
Function order is very important
47
contains(Key, []) ->
false;
contains(Key, List) ->
lists:any(fun(X) -> Key == X end, List).
contains(1, [1, 2, 3]).
true
contains({home, 123}, [{apartment, 333}, {house, 000}]).
false
48
Anonymous
49
contains(Key, []) ->
false;
contains(Key, List) ->
lists:any(fun(X) -> Key == X end, List).
F = fun(X, Y) ->
X1 = X * 2,
Y1 = Y * X,
X1 + Y1
end,
F(2, 2).
>> 12
50
Looping
Tail recursion makes looping good again.
51
print([]) ->
ok;
print([H|T]) ->
io:format(~p~n, [H]),
print(T).
52
Conditionals
Ifs are okay, cases are cooler.
53
if
A == 5 ->
do_something();
true ->
do_something_else()
end.
54
case A of
0 ->
first();
2 ->
second();
_ ->
something()
end.
55
Resources
http://www.erlang.org
Technorati
56
57
Questions?
58