introduction erlang conclusiontheory.stanford.edu/~mp/mp/teaching_files/2014-erl.pdfintroduction...
TRANSCRIPT
![Page 1: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/1.jpg)
IntroductionErlang
Conclusion
CPL - Erlang
Marco Patrignani
KU Leuven
24 October 2014
Marco Patrignani CPL - Erlang 1/36
![Page 2: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/2.jpg)
IntroductionErlang
Conclusion
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 2/36
![Page 3: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/3.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 3/36
![Page 4: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/4.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 5: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/5.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 6: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/6.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )
dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 7: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/7.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )
concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 8: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/8.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrent
fault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 9: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/9.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 10: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/10.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 11: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/11.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
What is Erlang?
programming language +runtime system +OTP (libraries for DBs, FFIs ...)
Programming language:
functional (like )dynamically typed
(unlike )concurrentfault-tolerant
Runtime system:
garbage collector
(like )
Marco Patrignani CPL - Erlang 4/36
![Page 12: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/12.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why using Erlang?
inherently concurrent programs:achieved via processes (no threads)
benefit from parallelisation (also thanks to SSA)
distributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime
Marco Patrignani CPL - Erlang 5/36
![Page 13: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/13.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why using Erlang?
inherently concurrent programs:achieved via processes (no threads)benefit from parallelisation (also thanks to SSA)
distributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime
Marco Patrignani CPL - Erlang 5/36
![Page 14: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/14.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why using Erlang?
inherently concurrent programs:achieved via processes (no threads)benefit from parallelisation (also thanks to SSA)
distributed programs on different machines
fault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime
Marco Patrignani CPL - Erlang 5/36
![Page 15: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/15.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why using Erlang?
inherently concurrent programs:achieved via processes (no threads)benefit from parallelisation (also thanks to SSA)
distributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crash
non-stop applications:ability to load code at runtime
Marco Patrignani CPL - Erlang 5/36
![Page 16: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/16.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why using Erlang?
inherently concurrent programs:achieved via processes (no threads)benefit from parallelisation (also thanks to SSA)
distributed programs on different machinesfault-tolerant systems:several mechanisms to recover faults without a system crashnon-stop applications:ability to load code at runtime
Marco Patrignani CPL - Erlang 5/36
![Page 17: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/17.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why NOT using Erlang?
Poor support for frontends/GUIs
Not as supported as other languages (unlike )Not known / understood as other languages
(unlike or C) (like )
Marco Patrignani CPL - Erlang 6/36
![Page 18: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/18.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why NOT using Erlang?
Poor support for frontends/GUIs
Not as supported as other languages (unlike )
Not known / understood as other languages
(unlike or C) (like )
Marco Patrignani CPL - Erlang 6/36
![Page 19: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/19.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Why NOT using Erlang?
Poor support for frontends/GUIs
Not as supported as other languages (unlike )Not known / understood as other languages
(unlike or C) (like )
Marco Patrignani CPL - Erlang 6/36
![Page 20: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/20.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Who uses Erlang?
Marco Patrignani CPL - Erlang 7/36
![Page 21: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/21.jpg)
IntroductionErlang
ConclusionWhat, who, why, using Erlang?
Who uses Erlang?
Marco Patrignani CPL - Erlang 7/36
![Page 22: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/22.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 8/36
![Page 23: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/23.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
Marco Patrignani CPL - Erlang 9/36
![Page 24: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/24.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
module definition
Marco Patrignani CPL - Erlang 9/36
![Page 25: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/25.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app) .
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
EVERYTHING ends with a dot “.”
Marco Patrignani CPL - Erlang 9/36
![Page 26: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/26.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num ,
Avg = average([Num , Local_Var]) ,
Sqr_Avg = math:sqrt(Avg) ,
io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
commas “,” separate things
Marco Patrignani CPL - Erlang 9/36
![Page 27: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/27.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
list of exported functions
Marco Patrignani CPL - Erlang 9/36
![Page 28: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/28.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export( [ func/1 ] ).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
list of exported functionslists are between square brackets “[ ] ”
Marco Patrignani CPL - Erlang 9/36
![Page 29: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/29.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func /1 ]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
list of exported functionsarity of the function
Marco Patrignani CPL - Erlang 9/36
![Page 30: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/30.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
function definition
Marco Patrignani CPL - Erlang 9/36
![Page 31: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/31.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
function definition
LOCAL function definition
Marco Patrignani CPL - Erlang 9/36
![Page 32: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/32.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
variable, single assignment, like
Marco Patrignani CPL - Erlang 9/36
![Page 33: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/33.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok .
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
variable, single assignment, like
atom
Marco Patrignani CPL - Erlang 9/36
![Page 34: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/34.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok .
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
variable, single assignment, like
atommind the difference!!
Marco Patrignani CPL - Erlang 9/36
![Page 35: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/35.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),
Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
local function call
Marco Patrignani CPL - Erlang 9/36
![Page 36: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/36.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),
Sqr_Avg = math:sqrt (Avg),
io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
local function callfunction call across modules
Marco Patrignani CPL - Erlang 9/36
![Page 37: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/37.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average ([Num, Local_Var]),
Sqr_Avg = math : sqrt (Avg),
io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
local function callfunction call across modules
Module name – colon “:” – function name
Marco Patrignani CPL - Erlang 9/36
![Page 38: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/38.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr(fun(El, Acc)-> El + Acc end , 0, L).
supports higher-order functions
(like , , )
Marco Patrignani CPL - Erlang 9/36
![Page 39: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/39.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Functional code
-module(app).
-export([func/1]).
func( Num )->
Local_Var = 2 * Num,Avg = average([Num, Local_Var]),Sqr_Avg = math:sqrt(Avg),io:format("Result ~p. ~n",[Sqr_Avg]),
ok.
average( L ) ->
lists:foldr( fun(El, Acc)-> El + Acc end , 0, L).
anonymous function (like , )
Marco Patrignani CPL - Erlang 9/36
![Page 40: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/40.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217
Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 41: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/41.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"
Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 42: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/42.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished values
Pids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 43: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/43.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifier
Funs: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 44: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/44.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 45: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/45.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 46: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/46.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang datatypes (selection of)
Integers: 1, 15 and -4217Strings: "You", "are", "sleeping"Atoms: distinguished valuesPids: process identifierFuns: function closures created by expressions:fun(...) ->... end.
Tuples: contain a fixed number data types:{E1, E2, ..., En}
Lists: [ Head | Tail ]. [] denotes an empty list.
Marco Patrignani CPL - Erlang 10/36
![Page 47: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/47.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Dynamic typing
NO static typing (unlike )
This is a valid erlang program (will fail at runtime)
add( X, Y ) ->
X + Y
end.
...
add( 5, "marco" ).
Marco Patrignani CPL - Erlang 11/36
![Page 48: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/48.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Dynamic typing
NO static typing (unlike )This is a valid erlang program (will fail at runtime)
add( X, Y ) ->
X + Y
end.
...
add( 5, "marco" ).
Marco Patrignani CPL - Erlang 11/36
![Page 49: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/49.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Compiling and running Erlang code
Download and install the runtime:http://www.erlang.org/download.html
erl starts the consoleerlc filename.erl compiles
run commands within the consolec(filename). compiles from the console
Marco Patrignani CPL - Erlang 12/36
![Page 50: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/50.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Compiling and running Erlang code
Download and install the runtime:http://www.erlang.org/download.html
erl starts the console
erlc filename.erl compiles
run commands within the consolec(filename). compiles from the console
Marco Patrignani CPL - Erlang 12/36
![Page 51: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/51.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Compiling and running Erlang code
Download and install the runtime:http://www.erlang.org/download.html
erl starts the consoleerlc filename.erl compiles
run commands within the consolec(filename). compiles from the console
Marco Patrignani CPL - Erlang 12/36
![Page 52: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/52.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Compiling and running Erlang code
Download and install the runtime:http://www.erlang.org/download.html
erl starts the consoleerlc filename.erl compiles
run commands within the console
c(filename). compiles from the console
Marco Patrignani CPL - Erlang 12/36
![Page 53: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/53.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Compiling and running Erlang code
Download and install the runtime:http://www.erlang.org/download.html
erl starts the consoleerlc filename.erl compiles
run commands within the consolec(filename). compiles from the console
Marco Patrignani CPL - Erlang 12/36
![Page 54: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/54.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Useful links
Erlang API: http://www.erlang.org/doc/man_index.htmlPart I of Concurrent programming in Erlang, by J. Armstrong,R. Virding, C. Wikström and M. Williams: http://www.erlang.org/download/erlang-book-part1.pdf
http:
//www.erlang.org/static/getting_started_quickly.html
http://www.erlang.org/doc/getting_started/users_
guide.html
http://learnyousomeerlang.com/
Marco Patrignani CPL - Erlang 13/36
![Page 55: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/55.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Coding time
Coding timelength/1,dynamic type error with length/1, atom_to_list BIF,filter/2,anonymous functions, guards in functions,tailFilter/2
Marco Patrignani CPL - Erlang 14/36
![Page 56: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/56.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 15/36
![Page 57: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/57.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The Actor model
everything is an actor
messages are the means of communication (asynchronous)actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)
Marco Patrignani CPL - Erlang 16/36
![Page 58: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/58.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The Actor model
everything is an actormessages are the means of communication (asynchronous)
actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)
Marco Patrignani CPL - Erlang 16/36
![Page 59: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/59.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The Actor model
everything is an actormessages are the means of communication (asynchronous)actors have mailboxes where messages are queued
actors send and receive messages (only 2 primitives)
Marco Patrignani CPL - Erlang 16/36
![Page 60: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/60.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The Actor model
everything is an actormessages are the means of communication (asynchronous)actors have mailboxes where messages are queuedactors send and receive messages (only 2 primitives)
Marco Patrignani CPL - Erlang 16/36
![Page 61: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/61.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Actors vs Threads
Actors
(generally) context switched bythe runtimemessage passing (asynchronous)no race conditons: no lockingcan deadlockbenefit from SSA
Threads
(generally) context switched bythe OSshared memory (sync/async)race conditions: needs lockingcan deadlockrely on mutable state
Marco Patrignani CPL - Erlang 17/36
![Page 62: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/62.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Actors vs Threads
Actors
(generally) context switched bythe runtimemessage passing (asynchronous)no race conditons: no lockingcan deadlockbenefit from SSA
Threads
(generally) context switched bythe OSshared memory (sync/async)race conditions: needs lockingcan deadlockrely on mutable state
Marco Patrignani CPL - Erlang 17/36
![Page 63: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/63.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang’s concurrency model, informally
Actor model:actors are lightweight processes, not OS processes, not threads
309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax.
has actors as a library
Akka implements actors for the JVM ( , ...)actor framework or Retlang for .Net
Marco Patrignani CPL - Erlang 18/36
![Page 64: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/64.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang’s concurrency model, informally
Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)
fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax.
has actors as a library
Akka implements actors for the JVM ( , ...)actor framework or Retlang for .Net
Marco Patrignani CPL - Erlang 18/36
![Page 65: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/65.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang’s concurrency model, informally
Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)
processes have mailboxessend and receive are part of syntax.
has actors as a library
Akka implements actors for the JVM ( , ...)actor framework or Retlang for .Net
Marco Patrignani CPL - Erlang 18/36
![Page 66: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/66.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang’s concurrency model, informally
Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxes
send and receive are part of syntax.has actors as a library
Akka implements actors for the JVM ( , ...)actor framework or Retlang for .Net
Marco Patrignani CPL - Erlang 18/36
![Page 67: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/67.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang’s concurrency model, informally
Actor model:actors are lightweight processes, not OS processes, not threads309 words of memory when spawned (very small!)fixed point context switches (optimal concurrency)processes have mailboxessend and receive are part of syntax.
has actors as a library
Akka implements actors for the JVM ( , ...)actor framework or Retlang for .Net
Marco Patrignani CPL - Erlang 18/36
![Page 68: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/68.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Receiving messages in Erlang
receive
mess ->
ok;{tuple, Number} ->
io:format("Received ~p",[Number])
after
10000 ->
ok
end.
Marco Patrignani CPL - Erlang 19/36
![Page 69: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/69.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Receiving messages in Erlang
receive
mess ->
ok;
{tuple, Number} ->
io:format("Received ~p",[Number])
after
10000 ->
ok
end.
pattern matching (like , )
Marco Patrignani CPL - Erlang 19/36
![Page 70: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/70.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Receiving messages in Erlang
receive
mess ->
ok ;
{tuple, Number} ->
io:format("Received ~p",[Number])
after
10000 ->
ok
end.
pattern matching (like , )separate by “;”
Marco Patrignani CPL - Erlang 19/36
![Page 71: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/71.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Receiving messages in Erlang
receive
mess ->
ok;{tuple, Number} ->
io:format("Received ~p",[Number])
after
10000 ->
ok
end.
pattern matching (like , )
last clause ends with NOTHING
Marco Patrignani CPL - Erlang 19/36
![Page 72: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/72.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Receiving messages in Erlang
receive
mess ->
ok;{tuple, Number} ->
io:format("Received ~p",[Number])
after
10000 ->
ok
end.
timeout clause
Marco Patrignani CPL - Erlang 19/36
![Page 73: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/73.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The receive loop
a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive
server( NumMess )->
receive
mess ->
server( NumMess + 1 );
{tuple, Number} ->
server( Number ) + 1
after
10000 ->
ok
end.
Marco Patrignani CPL - Erlang 20/36
![Page 74: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/74.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The receive loop
a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive
server( NumMess )->
receive
mess ->
server( NumMess + 1 );
{tuple, Number} ->
server( Number ) + 1
after
10000 ->
ok
end.
Marco Patrignani CPL - Erlang 20/36
![Page 75: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/75.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The receive loop
a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive
server( NumMess )->
receive
mess ->
server( NumMess + 1 ) ;
{tuple, Number} ->
server( Number ) + 1
after
10000 ->
ok
end.
this one is ok
Marco Patrignani CPL - Erlang 20/36
![Page 76: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/76.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The receive loop
a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive
server( NumMess )->
receive
mess ->
server( NumMess + 1 );
{tuple, Number} ->
server( Number ) + 1
after
10000 ->
ok
end.
this one is NOT
Marco Patrignani CPL - Erlang 20/36
![Page 77: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/77.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The receive loop
a receive loop is a recursive receive inside a functionthe recursive call must be tail-recursive
server( NumMess )->
receive
mess ->
server( NumMess + 1 );
{tuple, Number} ->
server( Number ) + 1
after
10000 ->
ok
end.
State of the function (unlike , C)
Marco Patrignani CPL - Erlang 20/36
![Page 78: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/78.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 79: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/79.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiers
processes are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 80: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/80.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PID
the BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 81: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/81.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current process
PIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 82: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/82.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)
local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 83: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/83.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Sending messages in Erlang
messages are sent to PIDs or NAMEs:PID !{mess, Var, 1}
PIDs are process identifiersprocesses are created with the BIF spawn/1-4, which returnsa PIDthe BIF self/0 returns the PID of the current processPIDs can be registered to local names with register/2
(good for servers and for failing-respawning processes)local names are fetch with registered/0
Marco Patrignani CPL - Erlang 21/36
![Page 84: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/84.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Coding time
Coding timereceive messages, send messages, timeoutspawn/3, register/2, unregister/1
receive loop
Marco Patrignani CPL - Erlang 22/36
![Page 85: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/85.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 23/36
![Page 86: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/86.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The “Let it crash” philosophy
expect failuredeal with it
Failures:in the same function: exceptions, errors and exits.
Handled with try / catch, like in andin another processHandled as messages by monitor/2 and link/1
Marco Patrignani CPL - Erlang 24/36
![Page 87: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/87.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The “Let it crash” philosophy
expect failuredeal with it
Failures:in the same function: exceptions, errors and exits.
Handled with try / catch, like in and
in another processHandled as messages by monitor/2 and link/1
Marco Patrignani CPL - Erlang 24/36
![Page 88: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/88.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
The “Let it crash” philosophy
expect failuredeal with it
Failures:in the same function: exceptions, errors and exits.
Handled with try / catch, like in andin another processHandled as messages by monitor/2 and link/1
Marco Patrignani CPL - Erlang 24/36
![Page 89: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/89.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Exceptions – throwing
Three types:
throw(Exception).
erlang:error(Reason).
exit(Reason).
Marco Patrignani CPL - Erlang 25/36
![Page 90: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/90.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Exceptions – throwing
Three types:
throw(Exception).
erlang:error(Reason).
exit(Reason).
Marco Patrignani CPL - Erlang 25/36
![Page 91: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/91.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Exceptions – throwing
Three types:
throw(Exception).
erlang:error(Reason).
exit(Reason).
Marco Patrignani CPL - Erlang 25/36
![Page 92: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/92.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Exceptions – catching
throws(F) ->
try F() of_ -> ok
catch
Throw -> {throw, caught, Throw};
error:Error -> {error, caught, Error};
exit:Exit -> {exit, caught, Exit}
end.
Marco Patrignani CPL - Erlang 26/36
![Page 93: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/93.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Monitoring
Unidirectionalreceive a message when a process dies:{ ’DOWN’, MonitorReference, process, Pid, Reason}
Pid = spawn( function ),
Ref = monitor(process, Pid).
... % or
{ Pid , Ref } = spawn_monitor( function ).
... %remove with
demonitor( Ref ).
Marco Patrignani CPL - Erlang 27/36
![Page 94: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/94.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Monitoring
Unidirectionalreceive a message when a process dies:{ ’DOWN’, MonitorReference, process, Pid, Reason}
Pid = spawn( function ),
Ref = monitor(process, Pid).
... % or
{ Pid , Ref } = spawn_monitor( function ).
... %remove with
demonitor( Ref ).
Marco Patrignani CPL - Erlang 27/36
![Page 95: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/95.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Linking
Bidirectionalreceive message when either process dies:{ ’EXIT’, Pid, Reason}only active after process_flag(trap_exit, true)
Pid = spawn( function ).
link( Pid ).
... % or
Pid = spawn_link( function ).
... %remove with
unlink( Pid ).
Marco Patrignani CPL - Erlang 28/36
![Page 96: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/96.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Linking
Bidirectionalreceive message when either process dies:{ ’EXIT’, Pid, Reason}only active after process_flag(trap_exit, true)
Pid = spawn( function ).
link( Pid ).
... % or
Pid = spawn_link( function ).
... %remove with
unlink( Pid ).
Marco Patrignani CPL - Erlang 28/36
![Page 97: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/97.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Coding time
Coding timetry/catchspawn_link/3, link/1, monitor/2
Marco Patrignani CPL - Erlang 29/36
![Page 98: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/98.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Outline
1 IntroductionWhat, who, why, using Erlang?
2 ErlangSyntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
3 Conclusion
Marco Patrignani CPL - Erlang 30/36
![Page 99: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/99.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
What is distribution?
processes on different Erlang nodes
different Erlang nodes on different machines
some applications are inherently distributede.g. Cloud management, load balancing middleware ...
Marco Patrignani CPL - Erlang 31/36
![Page 100: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/100.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
What is distribution?
processes on different Erlang nodesdifferent Erlang nodes on different machines
some applications are inherently distributede.g. Cloud management, load balancing middleware ...
Marco Patrignani CPL - Erlang 31/36
![Page 101: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/101.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
What is distribution?
processes on different Erlang nodesdifferent Erlang nodes on different machines
some applications are inherently distributede.g. Cloud management, load balancing middleware ...
Marco Patrignani CPL - Erlang 31/36
![Page 102: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/102.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang Nodes
an Erlang node is an executing Erlang system
a node is given a name erl -name asd
the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names
Marco Patrignani CPL - Erlang 32/36
![Page 103: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/103.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang Nodes
an Erlang node is an executing Erlang systema node is given a name erl -name asd
the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names
Marco Patrignani CPL - Erlang 32/36
![Page 104: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/104.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang Nodes
an Erlang node is an executing Erlang systema node is given a name erl -name asd
the BIF node/0 returns the full name
the BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names
Marco Patrignani CPL - Erlang 32/36
![Page 105: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/105.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Erlang Nodes
an Erlang node is an executing Erlang systema node is given a name erl -name asd
the BIF node/0 returns the full namethe BIFs spawn/1-4, monitor/2, link/1, register/2 allwork also with node names
Marco Patrignani CPL - Erlang 32/36
![Page 106: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/106.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Nodes
connect with net_kernel:connect_node( NodeName )
net_kernel coordinates distributed Erlang systemsuse cookies to prevent communicationsuse -hidden to prevent communications
Marco Patrignani CPL - Erlang 33/36
![Page 107: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/107.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Nodes
connect with net_kernel:connect_node( NodeName )
net_kernel coordinates distributed Erlang systems
use cookies to prevent communicationsuse -hidden to prevent communications
Marco Patrignani CPL - Erlang 33/36
![Page 108: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/108.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Nodes
connect with net_kernel:connect_node( NodeName )
net_kernel coordinates distributed Erlang systemsuse cookies to prevent communications
use -hidden to prevent communications
Marco Patrignani CPL - Erlang 33/36
![Page 109: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/109.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Nodes
connect with net_kernel:connect_node( NodeName )
net_kernel coordinates distributed Erlang systemsuse cookies to prevent communicationsuse -hidden to prevent communications
Marco Patrignani CPL - Erlang 33/36
![Page 110: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/110.jpg)
IntroductionErlang
Conclusion
Syntax and examplesConcurrency in ErlangLet it crashDistribution in Erlang
Coding time
Coding timedistributed communication
Marco Patrignani CPL - Erlang 34/36
![Page 111: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/111.jpg)
IntroductionErlang
Conclusion
Conclusion
Erlang is:concurrent (also parallel and distributed)functionalfail-resistant
good for backend softwaregood for long-lived applications
Marco Patrignani CPL - Erlang 35/36
![Page 112: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/112.jpg)
IntroductionErlang
Conclusion
Conclusion
Erlang is:concurrent (also parallel and distributed)functionalfail-resistant
good for backend software
good for long-lived applications
Marco Patrignani CPL - Erlang 35/36
![Page 113: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/113.jpg)
IntroductionErlang
Conclusion
Conclusion
Erlang is:concurrent (also parallel and distributed)functionalfail-resistant
good for backend softwaregood for long-lived applications
Marco Patrignani CPL - Erlang 35/36
![Page 114: Introduction Erlang Conclusiontheory.stanford.edu/~mp/mp/Teaching_files/2014-Erl.pdfIntroduction Erlang Conclusion Outline 1 Introduction What,who,why,usingErlang? 2 Erlang Syntaxandexamples](https://reader033.vdocument.in/reader033/viewer/2022050523/5fa69a28e40e3f533f46a984/html5/thumbnails/114.jpg)
IntroductionErlang
Conclusion
Homework and lab exercises
Find the homework exercise in Toledo.Lab sessions:
November 4, from 1:30 PM to 4:00 PM, Location:200A.SOL_ZNovember 14, from 10:30 AM to 1:00 PM, Location:200A.SOL_Z
DOyour homework before the lab sessions
Marco Patrignani CPL - Erlang 36/36