0% found this document useful (0 votes)
32 views95 pages

Erlang Webcast

This document provides an introduction to the Erlang programming language for Python programmers. It discusses what Erlang is, why one might want to learn it, and what problems it is well-suited for, such as building fault-tolerant systems that require extremely high uptime. It also acknowledges the "culture shock" of learning Erlang due to aspects like its unusual syntax, immutability, lack of looping constructs, recursion-based programming, and other differences from languages like Python. The document encourages persistence in overcoming Erlang's initial weirdness and provides some web resources for learning more.

Uploaded by

vskulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views95 pages

Erlang Webcast

This document provides an introduction to the Erlang programming language for Python programmers. It discusses what Erlang is, why one might want to learn it, and what problems it is well-suited for, such as building fault-tolerant systems that require extremely high uptime. It also acknowledges the "culture shock" of learning Erlang due to aspects like its unusual syntax, immutability, lack of looping constructs, recursion-based programming, and other differences from languages like Python. The document encourages persistence in overcoming Erlang's initial weirdness and provides some web resources for learning more.

Uploaded by

vskulkarni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

An Introduction to Erlang

for Python programmers

Paul Barry – Institute of Technology, Carlow in Ireland

PyCon Ireland 2011 - October 2011


Grab the slides:

http://paulbarry.itcarlow.ie/ErlangWebcast.pdf

2
Disclaimer

3
What exactly is Erlang?

4
“Erlang is a declarative,dynamically-typed,
functional, concurrent, distributed and
fault-tolerant programming language
with garbage collection and code
hot-swapping built into its runtime.”

Buzz-word city,
dude!

5
Scratching an itch in
the early '80s...

6
Why learn Erlang?

7
“Learn a new programming
language every year”

8
“Buy at least one new
programming book every year”

9
So, really, why learn Erlang?

10
A better programmer,
you will be...

11
Learn how others solve problems with a
different language, then apply these new
techniques to your current language

12
Learn what each language is good at,
then pick the right tool for the job

13
Works with a really big hammer!

14
What is Erlang good at?

15
What Erlang Wasn't Designed To Do

16
Erlang wasn't designed to...

Run in a browser

Process text efficiently

Be easy to teach

Be easy to learn

Build dynamic websites

Run on mobile phones

Allow non-programmers to program

Build GUIs
17
Erlang was designed to build
software systems that never stop

18
What exactly do you
mean by “never”?

19
“Never” at Ericsson means “no more
than 4 minutes downtime per year”...

http://www.infoq.com/presentations/Systems-that-Never-Stop-Joe-Armstrong 20
Wow! That's 5 nines
availability!*

21
* 99.999% uptime
Let's build software
that's 100% defect free!

22
Let's throw lots and lots
of code at the problem and
it'll go away, eh?

23
There has to be a better way

24
Let it crash!

Erlang programmers concentrate on


coding for the correct case
and crashing on failure

25
Robustness is achieved by having
programmers concentrate on processes and the
interactions (or messages) between them

26
Early error detection/recovery and the use
of concurrent programming techniques
lets you build fault tolerant systems

27
COP: Concurrency-Oriented Programming

28
The problem is...

29
The problem is...

Erlang is a little weird

30
Consider this code...
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

31
Strange punctuation symbols . ; ,
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

32
Lots of arrows -> in lots of places
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

33
Even stranger symbols - _ !
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

34
What's the deal with [] {} and () ?
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

35
Functions that call themselves
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end.

36
This is weird...

and there's even more

37
Erlang Culture Shock

38
Shock #1

Erlang's syntax is based on Prolog

-module(howdy).
-export([hi/1]).

hi(Name) ->
io:format('Hi there, ~p!~n', [Name]).

{person, First, Last} = {person, 'Paul', 'Barry'}.

howdy:hi(Last).
39
Shock #2

Erlang is not object-oriented

40
Shock #3

Everything in Erlang is immutable

Not just tuples and not just strings...

EVERYTHING

41
So... this doesn't work!

X = 10.
X = X + 1.

** exception error: no match of right hand side value 11

Y = X + 1.

42
Erlang's troublesome = operator

The “=” operator does not mean “assign”

It means “match” or “bind to”

43
No side effects here!

Destructive updates are forbidden

and (as an added twist)

Variables must start with an Uppercase letter:

X
Pid
Func
44
Shock #4

There are no built-in looping constructs

No for and no while

45
So... how do you loop?

46
List Comprehensions

Erlang:
Alist = [1, 2, 3, 4, 5].
Doubled = [X*2 || X <- Alist].

Python:
alist = [1, 2, 3, 4, 5]
doubled = [x*2 for x in alist]

Note: alist and doubled are mutable; Alist and Doubled are not

47
Shock #5

“Regular” looping is via recursion

48
Spot the loop?
-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()
49
end.
Shock #6

Strings are stored as a


list of integers

50
Sweet mother of all
things Erlang! Whose bright
idea was that?

51
Shock #7

There is no

if... then... else...

statement

52
There are if and case expressions
but... they're kinda weird

53
54
With enough pig-headed persistence,
weirdness can be overcome

55
So... who do I call to
help me learn Erlang?

56
57
58
Erlang is a language you study
as getting up-to-speed with
Erlang takes time

59
Web Resources

http://www.erlang.org

Try Erlang:

http://www.tryerlang.org/

“Learn Some Erlang For Great Good!”:

http://learnyousomeerlang.com/

The Erlang Factory:

http://erlang-factory.com/

InfoQ:

http://www.infoq.com
60
Some Unique “Characteristics”

61
Banned by Ericsson in 1998 for
“not being open enough”

62
63
http://video.google.com/videoplay?docid=-5830318882717959520
64
This is all great, but...
how exactly do I use
Erlang to build a
fault-tolerant system?

65
Learn Three Things


Create an Erlang process with spawn()


Send a message to a process with !


Process a message with receive

66
Remember this code?
%% Saved in 'hello_server.erl'.

-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end. 67
Create the hello() process

Pid = spawn(fun hello_server:hello/0).

<0.38.0>

68
Send a message to a process with !

Pid ! robert.

robert

69
A little twist

Pid ! {self(), robert}.

{<0.31.0>,robert}

70
Messages sent to a process with !
are received by receive

71
Can you spot the pattern match?
%% Saved in 'hello_server.erl'.

-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end. 72
The {FromPID, Who} tuple matches
%% Saved in 'hello_server.erl'.

-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end. 73
Send a message back...
%% Saved in 'hello_server.erl'.

-module(hello_server).

-export([hello/0]).

hello() ->

receive

{FromPID, Who} ->

case Who of

robert -> FromPID ! "Hello Robert.";

mike -> FromPID ! "Hello Mike.";

joe -> FromPID ! "Hello Joe.";

_ -> FromPID ! "I don't know you."

end,

hello()

end. 74
spawn, send (!) then receive

Pid = spawn(fun hello_server:hello/0),


Pid ! {self(), robert},
receive
Response ->
Response
end.

"Hello Robert."

75
Things get really interesting
when the processes are
linked together with spawn_link()
and the trap_exit signal

76
Processes that are linked together can
react appropriately to EXIT messages from
each other... and what happens next is
controlled by the programmer

77
Distributed Erlang

I need to spawn
hello/0 hello/0 is
on glasnost... registered
pbmac.itcarlow.ie as 'hr'

78
glasnost.itcarlow.ie
Pid = spawn(fun hello_server:hello/0),

becomes
Pid = spawn(‘[email protected]’, hello_server, hello, []),

79
Over time, you'll end up repeating
a lot of your process management code...

80
81
OTP is the jewel in Erlang's crown

82
So... if Erlang's so
cool, how come no one
is using it?

83
The TIOBE Index

84
85
Erlang in Telecoms

86
Erlang in Data

87
Erlang on the Web

88
Erlang in Gaming

“…Mochi Media is the world's


largest browser-based games
network, with more than 140 million
monthly active users and 15,000
games on nearly 40,000 publisher
websites…”

89
Check out Bob Ippolito’s Erlang-Factory talks!
More Erlang in Gaming

90
Thanks to Malcolm Dowse (of DemonWare, Dublin) for permission to use these
images, which were first delivered as part of Malcolm's talk to Erlang Factory London 2011.
So... is Erlang
worth learning?

91
Yes

92
Lots more to discover...


Fun with fun

Bit-strings and bit syntax

ETS and DETS

“Live” code updating

The Mnesia Distributed Database

The Standard Library

CEAN

93
Friendly Community

erlang-questions mailing list

94
Questions?

95

You might also like