SlideShare a Scribd company logo
What Is Erlang?What Is Erlang?
Erlang is a programming language for building highly parallel,
distributed, fault-tolerant systems.
To start the Erlang interpreter, type erl at the shell prompt. And do not
forget: there is a dot at the end of an Erlang statement, not a
semicolon.
Facts about ErlangFacts about Erlang
● “Shared-nothing” model: no shared variables, pure message passing
● Erlang processes run on nodes, nodes run on physical computers
● Pairs of processes can be linked to support termination notification
● Hot-swappable modules (dynamic on-the-fly code replacement)
● Mnesia: built-in distributed fault-tolerant database
● OTP (Open Telecom Platform), a set of libraries and procedures that
includes:
● A complete Web server
● A complete FTP server
● A CORBA ORB
Apach vs YawsApach vs Yaws
● Yaws is an Erlang-
based Web server
● The graph shows
thoughput (KB/sec)
vs. load.
● Apache (blue and
green) dies when
subject to a load of
ca. 4000 parallel
sessions.
Erlang execution modelErlang execution model
Sequential ErlangSequential Erlang
Programming Erlang: the Sequential Part 6
ArithmeticsArithmetics
● Erlang integer numbers are arbitrary-sized (there is no overflow)
● 16#19AC is a base 16 number, 32#SUGAR is a base 32 number, etc.
● / always produces a floating point value; rem and div perform integer
division. 5/2 is 2.5
● bnot, band, bor, bxor, bsl, bsl perform bitwise operations
Programming Erlang: the Sequential Part 7
VariablesVariables
● Variables in Erlang are single-assignment (immutable), like final in
Java. Single assignment does not cause side effects and makes
parallelism easy. Variable names begin with an uppercase letter.
● = is not assignment but matching: it matches the LHS against the RHS
and binds variables to the matching values. No weird math like
X=X+1.
● Function f() “forgets” all existing bindings.
Programming Erlang: the Sequential Part 8
ExamplesExamples
1> Oranges = 7.
7
2> Apples = 12.
12
3> Apples1 = Apples / 5 + Oranges.
9.4
4> Apples = Apples.
12
5> Oranges = Apples.
** exception error: no match of right hand side value
12
Programming Erlang: the Sequential Part 9
AtomsAtoms
● An atom is like a C/Java enum. It begins with a lowercase letter, can
consist of alphanumeric symbols, underscores _, and @. It can also be
any sequence of symbols in single quotation marks.
● The value of an atom is the atom itself.
Programming Erlang: the Sequential Part 10
ExamplesExamples
1> Apple.
* 1: variable 'Apple' is unbound
2> apple.
apple
3> 'Apple'.
'Apple'
4> apples@oranges.
apples@oranges.
Programming Erlang: the Sequential Part 11
TuplesTuples
● A tuple is a collection of values enclosed in curly braces {}.
● The tuple fields do not have to be homogeneous.
● Tuple fields can be extracted using pattern matching.
● Atom _ matches anything.
Programming Erlang: the Sequential Part 12
ExamplesExamples
1> Food = {{apples, 12}, {oranges, 32}}.
{{apples,12},{oranges,32}}
2> {_,{_,Noranges}} = Food.
{{apples,12}, {oranges,32}}
3> NOranges.
32
Programming Erlang: the Sequential Part 13
ListsLists
● A list is an ordered collection of values enclosed in square brackets [].
● A list has a head H (car) and a tail T (cdr). T must be a list (can be an
empty list []).
● Vertical bar | separates H from T: [ H | T ]
● List elements can be extracted by pattern matching.
● Always build lists by adding a head to a tail! Operator ++ is very
slow.
Programming Erlang: the Sequential Part 14
ExamplesExamples
1> Food = [{apples,12},{oranges,77},{foobar,3}].
[{apples,12},{oranges,77},{foobar,3}]
2> [Breakfast|MoreFood] = Food.
[{apples,12},{oranges,77},{foobar,3}]
3> Breakfast.
{apples,12}
4> {FruitForBreakfast,_} = Breakfast.
{apples,12}
5> FruitForBreakfast.
apples
Programming Erlang: the Sequential Part 15
Other OperatorsOther Operators
● =:= tests for equality
● Boolean operators: and, or, not, xor
● Short-circuit Boolean: orelse, andalso
● ++ (infix) appends lists (should be avoided)
● -- subtracts a list from a list
Programming Erlang: the Sequential Part 16
StringsStrings
● A string is a list of integers.
● A string can be also represented as a collection of printable symbols
enclosed into double quotes.
● Operator $ translates a character into its numerical value: $a is 97.
● [$h,$e,$l,$l,$o] is “hello.”
Programming Erlang: the Sequential Part 17
ModulesModules
● A module (mymod) is a logically complete piece of code in a file
(mymod.erl).
● Function c() compiles a module into bytecode: c(mymod). produces
file mymod.beam.
● Modules have attributes.
● Attribute -module(mymod). starts the module code.
● Attribute -import(othermod). imports a module.
● Attribute -export([list of funcs]). exports functions.
● Attributes must precede any other code.
Programming Erlang: the Sequential Part 18
FunctionsFunctions
● A function consists of one or more clauses with heads and bodies.
● The clauses are separated by a semicolon.
● The order of clauses matters.
● Clauses can be recursive.
● Functions must be declared in modules (not in the shell).
● Functions are polymorphic.
Programming Erlang: the Sequential Part 19
ExamplesExamples
-export[area/1].
area ({rectangle, H, W}) → W * H;
area ({circle, R}) → 3.14159 * R * R.
%% Another file!
-export[reduce/1,reduce/0].
reduce ([]) → 0;
reduce ([Head|Tail]) → someFuncOf (Head)
+ reduce (Tail).
%% Functions can be polymorphic.
reduce () → reduce ([]).
Programming Erlang: the Sequential Part 20
Anonymous Functions (Funs)Anonymous Functions (Funs)
● A function is a first-order object.
● It can be anonymous (like a closure)—a fun.
● Funs can be passed as parameters.
● Funs can be returned.
Programming Erlang: the Sequential Part 21
ExamplesExamples
%% file apply.erl
-export([apply1/2]).
apply1 (Func, X) → Func(X).
%% shell
1> apply.apply1 (fun(X)→X*X end,77).
5929
2> Invert = fun(X) → bnot X end.
#Fun<erl_eval.6.13229925>
3> apply:apply1 (Invert, 77).
-78
4> Invert(16).
17
Programming Erlang: the Sequential Part 22
Module “lists”Module “lists”
● Implicitly imported.
● lists:map/2 applies a function to a list and returns a new list
● lists:filter/2 returns a list of elements for which a function returns true
● lists:seq/2 returns a list of sequential integer numbers in a range
● lists:reverse/1 reverses a list (highly optimized!)
Programming Erlang: the Sequential Part 23
ExamplesExamples
1> lists:filter (fun (X) → X div 2 =:= X-1 end,
[1,2,3,4,5,6,7]).
[1,2]
2> lists:map (fun (X) → X*X end, [1,2,3,4,5,6,7]).
[1,4,9,16,25,36,49]
Programming Erlang: the Sequential Part 24
List ComprehensionList Comprehension
● List comprehensions are expression to create lists by rules
● A comprehension consists of a constructor and qualifiers (one or
more)
● A qualifier is a filter (a predicate or a boolean expression) or a
generator of the form Pattern←List
Programming Erlang: the Sequential Part 25
ExamplesExamples
1> [X*X || X←[1,2,3,4,5,6,7,8]].
[1,4,9,16,25,36,49,64]
2> [X*X || X←lists:seq (1,8)].
[1,4,9,16,25,36,49,64]
%% qsort
qsort ([])→[];
qsort ([Pivot|T]) → qsort ([X || X ← T, X < Pivot] ++
[Pivot] ++ qsort ([X || X←T, X >= Pivot]).
%% permutations
perms ([]) → [[]];
perms (L) → [[H|T] || H ← L, T ← perms (L--[H])].
Programming Erlang: the Sequential Part 26
Block ExpressionsBlock Expressions
● Used when more than one expression is needed to produce the value
(like progn in Lisp)
● The value of the block is the value of the last expression:
begin
expr1,
expr2,
expr3
end
Programming Erlang: the Sequential Part 27
GuardsGuards
● Guard sequence is a series of guards separated by “;” (meaning “or”)
● Guard is a sequence of guard expressions separated by “,” (meaning
“and”)
● Guard expression is true, constant (=false), guard predicate (is_atom,
is_number, etc.), guard built-in function (abs, float, etc.), term
comparison (/=,==,=/=,=:=,etc.), arithmetic expression or (short-
circuit) boolean expression
Programming Erlang: the Sequential Part 28
ExamplesExamples
small(X) when X=:=0 orelse 1/abs(X) > 1000 → true;
small(X) → false;
max(X,Y) when X>Y → X;
max(X,Y) when true → Y. %% when true is optional
… when is_tuple(X), size(X)=:=6, abs(element(3,X))>5
element(4,X)=:=hd(L) …
Programming Erlang: the Sequential Part 29
RecordsRecords
● Records are tuples with named elements.
● They are declared with -record() attribute.
● They are usually stored in *.hrl files.
● Those files are included with the -include(). attribute or
with rr() shell command
● Records are instantiated with the # operator with or without
default values
● Records can be copied with changes
● Fields can be extracted by matching
Programming Erlang: the Sequential Part 30
ExamplesExamples
%% File “student.hrl”
-record(student,{name,age,class=freshman}).
%% File “main.erl”
-include(“student.hrl”).
BestStudent = #student{}.
SeniorStudent = #student{class=senior}.
AnotherSenior = BestStudent#student{age=17}.
#student{age=Age} = AnotherSenior.
Age. %% Will print 17
Programming Erlang: the Sequential Part 31
ExamplesExamples
clear_status (#todo{status=S,who=W})=R) →
R#todo{status=finished}.
Programming Erlang: the Sequential Part 32
Conditional statementsConditional statements
case Expression of
Pattern1 [when Guard1] → seq1;
Pattern2 [when Guard2] → seq2;
end
if
Guard1 → sequence1;
Guard2 → sequence2;
end
If there is no match, an exception is raised. Atom true matches everything.
Programming Erlang: the Sequential Part 33
ExamplesExamples
%% Split a lists into odd and even elements
odds_and_evens (L) →
odds_and_evens (L,[],[]).
odds_and_evens ([H|T],Odds,Evens) →
case (H rem 2) of
1 → odds_and_evens (T,[H|Odds],Evens);
0 → odds_and_evens (T, Odds, [H, Evens])
end
odds_and_evens ([], Odds, Evens) →
{lists:reverse (Odds), lists:reverse (Evens)}.
Programming Erlang: the Sequential Part 34
● There are three types of errors in Erlang:
● exit(Why) broadcasts {'EXIT',Pid,Why} to all linked processes
● throw(Why) throws an exception
● erlang:error(Why) terminates the current process immediately
Raising ExceptionsRaising Exceptions
Programming Erlang: the Sequential Part 35
Catching an ExceptionCatching an Exception
try FuncOrExprSeq
%% This part can be omitted
of
Pattern1 [when Guard1] → Expression1;
...
catch
%% Type can be throw (default), exit, error or _:_
ExType1: ExPattern1 [when ExGuard1] → ExExpr1;
...
after %% Guaranteed to execute!
AfterExpression
end
Programming Erlang: the Sequential Part 36
ExampleExample
try math:sqrt(-1)
catch
error: {badarith, _} → ....
end
Programming Erlang: the Sequential Part 37
Process DictionaryProcess Dictionary
● Each process has a private dictionary: an associative array
● Built-in functions (BIFs) to work with dictionaries:
● @spec put (Key, Value) → OldValue.
● @spec get (Key) → Value | undefined.
● @spec get_keys () → [Key].
● @spec get () → [{Key, Value}].
● @spec erase (Key) → Value.
● @spec erase() → [{Key, Value}].
Programming Erlang: the Sequential Part 38
MacrosMacros
● Defined using -define(). attribute.
● Called using ?.
● Parameters allowed.
● Predefined macros: ?FILE, ?MODULE, ?LINE.
● Other attributes: -ifdef()., -undef()., -ifndef()., -else., -endif.
Programming Erlang: the Sequential Part 39
ExamplesExamples
%% To be used in binaries
-define (DWORD, 32/unsigned-little-integer).
?DWORD.
%% Simple macro with parameters
-ifndef (isodd).
-define (isodd (X), X rem 2 =:= 1).
-endif.
?isodd (77).
Programming Erlang: the Sequential Part 40
This presentation roughly covers the first 27
pages (of the total of 29
) of
“Programming Erlang. Software for a Concurrent World,” by Joe
Armstrong.

More Related Content

What's hot

Java operators
Java operatorsJava operators
Java operators
Shehrevar Davierwala
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
Ganesh Samarthyam
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Scott Leberknight
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
RichardWarburton
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesMalik Tauqir Hasan
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
VandanaPagar1
 
02basics
02basics02basics
02basics
Waheed Warraich
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
Upender Upr
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
Logan Chien
 
Functional solid
Functional solidFunctional solid
Functional solid
Matt Stine
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Susan Potter
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
Kelsey Gilmore-Innis
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
Mario Fusco
 
Arduino - Classes and functions
Arduino - Classes and functionsArduino - Classes and functions
Arduino - Classes and functions
Emertxe Information Technologies Pvt Ltd
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingMario Fusco
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Mario Fusco
 

What's hot (20)

Java operators
Java operatorsJava operators
Java operators
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8Pragmatic functional refactoring with java 8
Pragmatic functional refactoring with java 8
 
Fpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directivesFpga 06-data-types-system-tasks-compiler-directives
Fpga 06-data-types-system-tasks-compiler-directives
 
Crash course in verilog
Crash course in verilogCrash course in verilog
Crash course in verilog
 
VHDL- gate level modelling
VHDL- gate level modellingVHDL- gate level modelling
VHDL- gate level modelling
 
02basics
02basics02basics
02basics
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
Functional solid
Functional solidFunctional solid
Functional solid
 
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
Scalaz By Example (An IO Taster) -- PDXScala Meetup Jan 2014
 
Learning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a NeckbeardLearning Functional Programming Without Growing a Neckbeard
Learning Functional Programming Without Growing a Neckbeard
 
Java 8 Workshop
Java 8 WorkshopJava 8 Workshop
Java 8 Workshop
 
Arduino - Classes and functions
Arduino - Classes and functionsArduino - Classes and functions
Arduino - Classes and functions
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
Java Generics
Java GenericsJava Generics
Java Generics
 
Why we cannot ignore Functional Programming
Why we cannot ignore Functional ProgrammingWhy we cannot ignore Functional Programming
Why we cannot ignore Functional Programming
 
Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...Laziness, trampolines, monoids and other functional amenities: this is not yo...
Laziness, trampolines, monoids and other functional amenities: this is not yo...
 
Meta Object Protocols
Meta Object ProtocolsMeta Object Protocols
Meta Object Protocols
 

Viewers also liked

Heroes 1 Monstros
Heroes 1 MonstrosHeroes 1 Monstros
Heroes 1 Monstros
leaderlock
 
Verdi in Venice
Verdi in VeniceVerdi in Venice
Verdi in Venicemariane m
 
Agile crash course - how to build bad software
Agile crash course - how to build bad softwareAgile crash course - how to build bad software
Agile crash course - how to build bad software
Simba Sagwete
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
Dmitry Zinoviev
 
Margravine Menaces
Margravine MenacesMargravine Menaces
Margravine Menaces
CoralieApril
 
Soviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success PredictorsSoviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success Predictors
Dmitry Zinoviev
 
Green rootz
Green rootz Green rootz
Green rootz
Green Rootz
 
October 2016 Newsletter
October 2016 NewsletterOctober 2016 Newsletter
October 2016 Newsletter
Chris Norton
 
Circulação lei estadual - 2013 - proposta patrocínio
Circulação   lei estadual - 2013 - proposta patrocínioCirculação   lei estadual - 2013 - proposta patrocínio
Circulação lei estadual - 2013 - proposta patrocínio
Rodolfo Monteiro
 
Selayang Pandang Ethical Fashion
Selayang Pandang Ethical FashionSelayang Pandang Ethical Fashion
Selayang Pandang Ethical Fashion
Sadikin Gani
 
Trabalho de gegrafia
Trabalho de gegrafiaTrabalho de gegrafia
Trabalho de gegrafia
Karina Bernardo Vieira
 
Beam me up, Scotty
Beam me up, ScottyBeam me up, Scotty
Beam me up, Scotty
Gianluca Padovani
 
QCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
QCON SP 2016 - Elixir: Tolerância a Falhas para AdultosQCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
QCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
Fabio Akita
 
Functional Programing
Functional ProgramingFunctional Programing
Functional Programing
Max Arshinov
 
end user programming & yahoo pipes
end user programming & yahoo pipesend user programming & yahoo pipes
end user programming & yahoo pipes
Bhasker Kode
 
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, HelpshiftKafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
Bhasker Kode
 
in-memory capacity planning, Erlang Factory London 09
in-memory capacity planning, Erlang Factory London 09in-memory capacity planning, Erlang Factory London 09
in-memory capacity planning, Erlang Factory London 09
Bhasker Kode
 
Parsing binaries and protocols with erlang
Parsing binaries and protocols with erlangParsing binaries and protocols with erlang
Parsing binaries and protocols with erlang
Bhasker Kode
 

Viewers also liked (20)

Heroes 1 Monstros
Heroes 1 MonstrosHeroes 1 Monstros
Heroes 1 Monstros
 
Verdi in Venice
Verdi in VeniceVerdi in Venice
Verdi in Venice
 
Agile crash course - how to build bad software
Agile crash course - how to build bad softwareAgile crash course - how to build bad software
Agile crash course - how to build bad software
 
CVPK2015
CVPK2015CVPK2015
CVPK2015
 
C for Java programmers (part 2)
C for Java programmers (part 2)C for Java programmers (part 2)
C for Java programmers (part 2)
 
Baiuteii
BaiuteiiBaiuteii
Baiuteii
 
Margravine Menaces
Margravine MenacesMargravine Menaces
Margravine Menaces
 
Soviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success PredictorsSoviet Popular Music Landscape: Community Structure and Success Predictors
Soviet Popular Music Landscape: Community Structure and Success Predictors
 
Green rootz
Green rootz Green rootz
Green rootz
 
October 2016 Newsletter
October 2016 NewsletterOctober 2016 Newsletter
October 2016 Newsletter
 
Circulação lei estadual - 2013 - proposta patrocínio
Circulação   lei estadual - 2013 - proposta patrocínioCirculação   lei estadual - 2013 - proposta patrocínio
Circulação lei estadual - 2013 - proposta patrocínio
 
Selayang Pandang Ethical Fashion
Selayang Pandang Ethical FashionSelayang Pandang Ethical Fashion
Selayang Pandang Ethical Fashion
 
Trabalho de gegrafia
Trabalho de gegrafiaTrabalho de gegrafia
Trabalho de gegrafia
 
Beam me up, Scotty
Beam me up, ScottyBeam me up, Scotty
Beam me up, Scotty
 
QCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
QCON SP 2016 - Elixir: Tolerância a Falhas para AdultosQCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
QCON SP 2016 - Elixir: Tolerância a Falhas para Adultos
 
Functional Programing
Functional ProgramingFunctional Programing
Functional Programing
 
end user programming & yahoo pipes
end user programming & yahoo pipesend user programming & yahoo pipes
end user programming & yahoo pipes
 
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, HelpshiftKafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
Kafka & Storm - FifthElephant 2015 by @bhaskerkode, Helpshift
 
in-memory capacity planning, Erlang Factory London 09
in-memory capacity planning, Erlang Factory London 09in-memory capacity planning, Erlang Factory London 09
in-memory capacity planning, Erlang Factory London 09
 
Parsing binaries and protocols with erlang
Parsing binaries and protocols with erlangParsing binaries and protocols with erlang
Parsing binaries and protocols with erlang
 

Similar to Introduction to Erlang Part 1

Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
Patrick Huesler
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
mohamedsamyali
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Hamidreza Soleimani
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
Nithin Kumar Singani
 
Erlang intro
Erlang introErlang intro
Erlang introSSA KPI
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
aboma2hawi
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Winl xf
 
Java 8
Java 8Java 8
Java 8
vilniusjug
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
jbp4444
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
MOHAMMAD SAYDUL ALAM
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
Ryan Brown
 
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
siouxhotornot
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
Mohamed Fawzy
 
Perl
PerlPerl
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaAyush Mishra
 
Erlang
ErlangErlang
Erlang
Balaji G
 
Keynote joearmstrong
Keynote joearmstrongKeynote joearmstrong
Keynote joearmstrongSentifi
 

Similar to Introduction to Erlang Part 1 (20)

Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
Architecting Scalable Platforms in Erlang/OTP | Hamidreza Soleimani | Diginex...
 
Perl Basics with Examples
Perl Basics with ExamplesPerl Basics with Examples
Perl Basics with Examples
 
Erlang intro
Erlang introErlang intro
Erlang intro
 
cp05.pptx
cp05.pptxcp05.pptx
cp05.pptx
 
Matlab-3.pptx
Matlab-3.pptxMatlab-3.pptx
Matlab-3.pptx
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 
Erlang Message Passing Concurrency, For The Win
Erlang  Message  Passing  Concurrency,  For  The  WinErlang  Message  Passing  Concurrency,  For  The  Win
Erlang Message Passing Concurrency, For The Win
 
Java 8
Java 8Java 8
Java 8
 
Intro to OpenMP
Intro to OpenMPIntro to OpenMP
Intro to OpenMP
 
1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx1. Ch_1 SL_1_Intro to Matlab.pptx
1. Ch_1 SL_1_Intro to Matlab.pptx
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
 
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
Sioux Hot-or-Not: Functional programming: unlocking the real power of multi-c...
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
Perl
PerlPerl
Perl
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Knolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in ScalaKnolx Session : Built-In Control Structures in Scala
Knolx Session : Built-In Control Structures in Scala
 
Erlang
ErlangErlang
Erlang
 
Keynote joearmstrong
Keynote joearmstrongKeynote joearmstrong
Keynote joearmstrong
 

More from Dmitry Zinoviev

Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)
Dmitry Zinoviev
 
WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?
Dmitry Zinoviev
 
The “Musk” Effect at Twitter
The “Musk” Effect at TwitterThe “Musk” Effect at Twitter
The “Musk” Effect at Twitter
Dmitry Zinoviev
 
Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?
Dmitry Zinoviev
 
Using Complex Network Analysis for Periodization
Using Complex Network Analysis for PeriodizationUsing Complex Network Analysis for Periodization
Using Complex Network Analysis for Periodization
Dmitry Zinoviev
 
Algorithms
AlgorithmsAlgorithms
Algorithms
Dmitry Zinoviev
 
Text analysis of The Book Club Play
Text analysis of The Book Club PlayText analysis of The Book Club Play
Text analysis of The Book Club Play
Dmitry Zinoviev
 
Exploring the History of Mental Stigma
Exploring the History of Mental StigmaExploring the History of Mental Stigma
Exploring the History of Mental Stigma
Dmitry Zinoviev
 
Roles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction NetworkRoles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction Network
Dmitry Zinoviev
 
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
Dmitry Zinoviev
 
Network analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweetsNetwork analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweets
Dmitry Zinoviev
 
Network Analysis of The Shining
Network Analysis of The ShiningNetwork Analysis of The Shining
Network Analysis of The Shining
Dmitry Zinoviev
 
The Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network AnalysisThe Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network Analysis
Dmitry Zinoviev
 
Pickling and CSV
Pickling and CSVPickling and CSV
Pickling and CSV
Dmitry Zinoviev
 
Python overview
Python overviewPython overview
Python overview
Dmitry Zinoviev
 
Welcome to CS310!
Welcome to CS310!Welcome to CS310!
Welcome to CS310!
Dmitry Zinoviev
 
Programming languages
Programming languagesProgramming languages
Programming languages
Dmitry Zinoviev
 
The P4 of Networkacy
The P4 of NetworkacyThe P4 of Networkacy
The P4 of Networkacy
Dmitry Zinoviev
 
DaVinci Code. Network Analysis
DaVinci Code. Network AnalysisDaVinci Code. Network Analysis
DaVinci Code. Network Analysis
Dmitry Zinoviev
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
Dmitry Zinoviev
 

More from Dmitry Zinoviev (20)

Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)Machine Learning Basics for Dummies (no math!)
Machine Learning Basics for Dummies (no math!)
 
WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?WHat is star discourse in post-Soviet film journals?
WHat is star discourse in post-Soviet film journals?
 
The “Musk” Effect at Twitter
The “Musk” Effect at TwitterThe “Musk” Effect at Twitter
The “Musk” Effect at Twitter
 
Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?Are Twitter Networks of Regional Entrepreneurs Gendered?
Are Twitter Networks of Regional Entrepreneurs Gendered?
 
Using Complex Network Analysis for Periodization
Using Complex Network Analysis for PeriodizationUsing Complex Network Analysis for Periodization
Using Complex Network Analysis for Periodization
 
Algorithms
AlgorithmsAlgorithms
Algorithms
 
Text analysis of The Book Club Play
Text analysis of The Book Club PlayText analysis of The Book Club Play
Text analysis of The Book Club Play
 
Exploring the History of Mental Stigma
Exploring the History of Mental StigmaExploring the History of Mental Stigma
Exploring the History of Mental Stigma
 
Roles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction NetworkRoles and Words in a massive NSSI-Related Interaction Network
Roles and Words in a massive NSSI-Related Interaction Network
 
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
“A Quaint and Curious Volume of Forgotten Lore,” or an Exercise in Digital Hu...
 
Network analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweetsNetwork analysis of the 2016 USA presidential campaign tweets
Network analysis of the 2016 USA presidential campaign tweets
 
Network Analysis of The Shining
Network Analysis of The ShiningNetwork Analysis of The Shining
Network Analysis of The Shining
 
The Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network AnalysisThe Lord of the Ring. A Network Analysis
The Lord of the Ring. A Network Analysis
 
Pickling and CSV
Pickling and CSVPickling and CSV
Pickling and CSV
 
Python overview
Python overviewPython overview
Python overview
 
Welcome to CS310!
Welcome to CS310!Welcome to CS310!
Welcome to CS310!
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
The P4 of Networkacy
The P4 of NetworkacyThe P4 of Networkacy
The P4 of Networkacy
 
DaVinci Code. Network Analysis
DaVinci Code. Network AnalysisDaVinci Code. Network Analysis
DaVinci Code. Network Analysis
 
C for Java programmers (part 3)
C for Java programmers (part 3)C for Java programmers (part 3)
C for Java programmers (part 3)
 

Recently uploaded

Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Globus
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 

Recently uploaded (20)

Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
Climate Science Flows: Enabling Petabyte-Scale Climate Analysis with the Eart...
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 

Introduction to Erlang Part 1

  • 1. What Is Erlang?What Is Erlang? Erlang is a programming language for building highly parallel, distributed, fault-tolerant systems. To start the Erlang interpreter, type erl at the shell prompt. And do not forget: there is a dot at the end of an Erlang statement, not a semicolon.
  • 2. Facts about ErlangFacts about Erlang ● “Shared-nothing” model: no shared variables, pure message passing ● Erlang processes run on nodes, nodes run on physical computers ● Pairs of processes can be linked to support termination notification ● Hot-swappable modules (dynamic on-the-fly code replacement) ● Mnesia: built-in distributed fault-tolerant database ● OTP (Open Telecom Platform), a set of libraries and procedures that includes: ● A complete Web server ● A complete FTP server ● A CORBA ORB
  • 3. Apach vs YawsApach vs Yaws ● Yaws is an Erlang- based Web server ● The graph shows thoughput (KB/sec) vs. load. ● Apache (blue and green) dies when subject to a load of ca. 4000 parallel sessions.
  • 6. Programming Erlang: the Sequential Part 6 ArithmeticsArithmetics ● Erlang integer numbers are arbitrary-sized (there is no overflow) ● 16#19AC is a base 16 number, 32#SUGAR is a base 32 number, etc. ● / always produces a floating point value; rem and div perform integer division. 5/2 is 2.5 ● bnot, band, bor, bxor, bsl, bsl perform bitwise operations
  • 7. Programming Erlang: the Sequential Part 7 VariablesVariables ● Variables in Erlang are single-assignment (immutable), like final in Java. Single assignment does not cause side effects and makes parallelism easy. Variable names begin with an uppercase letter. ● = is not assignment but matching: it matches the LHS against the RHS and binds variables to the matching values. No weird math like X=X+1. ● Function f() “forgets” all existing bindings.
  • 8. Programming Erlang: the Sequential Part 8 ExamplesExamples 1> Oranges = 7. 7 2> Apples = 12. 12 3> Apples1 = Apples / 5 + Oranges. 9.4 4> Apples = Apples. 12 5> Oranges = Apples. ** exception error: no match of right hand side value 12
  • 9. Programming Erlang: the Sequential Part 9 AtomsAtoms ● An atom is like a C/Java enum. It begins with a lowercase letter, can consist of alphanumeric symbols, underscores _, and @. It can also be any sequence of symbols in single quotation marks. ● The value of an atom is the atom itself.
  • 10. Programming Erlang: the Sequential Part 10 ExamplesExamples 1> Apple. * 1: variable 'Apple' is unbound 2> apple. apple 3> 'Apple'. 'Apple' 4> apples@oranges. apples@oranges.
  • 11. Programming Erlang: the Sequential Part 11 TuplesTuples ● A tuple is a collection of values enclosed in curly braces {}. ● The tuple fields do not have to be homogeneous. ● Tuple fields can be extracted using pattern matching. ● Atom _ matches anything.
  • 12. Programming Erlang: the Sequential Part 12 ExamplesExamples 1> Food = {{apples, 12}, {oranges, 32}}. {{apples,12},{oranges,32}} 2> {_,{_,Noranges}} = Food. {{apples,12}, {oranges,32}} 3> NOranges. 32
  • 13. Programming Erlang: the Sequential Part 13 ListsLists ● A list is an ordered collection of values enclosed in square brackets []. ● A list has a head H (car) and a tail T (cdr). T must be a list (can be an empty list []). ● Vertical bar | separates H from T: [ H | T ] ● List elements can be extracted by pattern matching. ● Always build lists by adding a head to a tail! Operator ++ is very slow.
  • 14. Programming Erlang: the Sequential Part 14 ExamplesExamples 1> Food = [{apples,12},{oranges,77},{foobar,3}]. [{apples,12},{oranges,77},{foobar,3}] 2> [Breakfast|MoreFood] = Food. [{apples,12},{oranges,77},{foobar,3}] 3> Breakfast. {apples,12} 4> {FruitForBreakfast,_} = Breakfast. {apples,12} 5> FruitForBreakfast. apples
  • 15. Programming Erlang: the Sequential Part 15 Other OperatorsOther Operators ● =:= tests for equality ● Boolean operators: and, or, not, xor ● Short-circuit Boolean: orelse, andalso ● ++ (infix) appends lists (should be avoided) ● -- subtracts a list from a list
  • 16. Programming Erlang: the Sequential Part 16 StringsStrings ● A string is a list of integers. ● A string can be also represented as a collection of printable symbols enclosed into double quotes. ● Operator $ translates a character into its numerical value: $a is 97. ● [$h,$e,$l,$l,$o] is “hello.”
  • 17. Programming Erlang: the Sequential Part 17 ModulesModules ● A module (mymod) is a logically complete piece of code in a file (mymod.erl). ● Function c() compiles a module into bytecode: c(mymod). produces file mymod.beam. ● Modules have attributes. ● Attribute -module(mymod). starts the module code. ● Attribute -import(othermod). imports a module. ● Attribute -export([list of funcs]). exports functions. ● Attributes must precede any other code.
  • 18. Programming Erlang: the Sequential Part 18 FunctionsFunctions ● A function consists of one or more clauses with heads and bodies. ● The clauses are separated by a semicolon. ● The order of clauses matters. ● Clauses can be recursive. ● Functions must be declared in modules (not in the shell). ● Functions are polymorphic.
  • 19. Programming Erlang: the Sequential Part 19 ExamplesExamples -export[area/1]. area ({rectangle, H, W}) → W * H; area ({circle, R}) → 3.14159 * R * R. %% Another file! -export[reduce/1,reduce/0]. reduce ([]) → 0; reduce ([Head|Tail]) → someFuncOf (Head) + reduce (Tail). %% Functions can be polymorphic. reduce () → reduce ([]).
  • 20. Programming Erlang: the Sequential Part 20 Anonymous Functions (Funs)Anonymous Functions (Funs) ● A function is a first-order object. ● It can be anonymous (like a closure)—a fun. ● Funs can be passed as parameters. ● Funs can be returned.
  • 21. Programming Erlang: the Sequential Part 21 ExamplesExamples %% file apply.erl -export([apply1/2]). apply1 (Func, X) → Func(X). %% shell 1> apply.apply1 (fun(X)→X*X end,77). 5929 2> Invert = fun(X) → bnot X end. #Fun<erl_eval.6.13229925> 3> apply:apply1 (Invert, 77). -78 4> Invert(16). 17
  • 22. Programming Erlang: the Sequential Part 22 Module “lists”Module “lists” ● Implicitly imported. ● lists:map/2 applies a function to a list and returns a new list ● lists:filter/2 returns a list of elements for which a function returns true ● lists:seq/2 returns a list of sequential integer numbers in a range ● lists:reverse/1 reverses a list (highly optimized!)
  • 23. Programming Erlang: the Sequential Part 23 ExamplesExamples 1> lists:filter (fun (X) → X div 2 =:= X-1 end, [1,2,3,4,5,6,7]). [1,2] 2> lists:map (fun (X) → X*X end, [1,2,3,4,5,6,7]). [1,4,9,16,25,36,49]
  • 24. Programming Erlang: the Sequential Part 24 List ComprehensionList Comprehension ● List comprehensions are expression to create lists by rules ● A comprehension consists of a constructor and qualifiers (one or more) ● A qualifier is a filter (a predicate or a boolean expression) or a generator of the form Pattern←List
  • 25. Programming Erlang: the Sequential Part 25 ExamplesExamples 1> [X*X || X←[1,2,3,4,5,6,7,8]]. [1,4,9,16,25,36,49,64] 2> [X*X || X←lists:seq (1,8)]. [1,4,9,16,25,36,49,64] %% qsort qsort ([])→[]; qsort ([Pivot|T]) → qsort ([X || X ← T, X < Pivot] ++ [Pivot] ++ qsort ([X || X←T, X >= Pivot]). %% permutations perms ([]) → [[]]; perms (L) → [[H|T] || H ← L, T ← perms (L--[H])].
  • 26. Programming Erlang: the Sequential Part 26 Block ExpressionsBlock Expressions ● Used when more than one expression is needed to produce the value (like progn in Lisp) ● The value of the block is the value of the last expression: begin expr1, expr2, expr3 end
  • 27. Programming Erlang: the Sequential Part 27 GuardsGuards ● Guard sequence is a series of guards separated by “;” (meaning “or”) ● Guard is a sequence of guard expressions separated by “,” (meaning “and”) ● Guard expression is true, constant (=false), guard predicate (is_atom, is_number, etc.), guard built-in function (abs, float, etc.), term comparison (/=,==,=/=,=:=,etc.), arithmetic expression or (short- circuit) boolean expression
  • 28. Programming Erlang: the Sequential Part 28 ExamplesExamples small(X) when X=:=0 orelse 1/abs(X) > 1000 → true; small(X) → false; max(X,Y) when X>Y → X; max(X,Y) when true → Y. %% when true is optional … when is_tuple(X), size(X)=:=6, abs(element(3,X))>5 element(4,X)=:=hd(L) …
  • 29. Programming Erlang: the Sequential Part 29 RecordsRecords ● Records are tuples with named elements. ● They are declared with -record() attribute. ● They are usually stored in *.hrl files. ● Those files are included with the -include(). attribute or with rr() shell command ● Records are instantiated with the # operator with or without default values ● Records can be copied with changes ● Fields can be extracted by matching
  • 30. Programming Erlang: the Sequential Part 30 ExamplesExamples %% File “student.hrl” -record(student,{name,age,class=freshman}). %% File “main.erl” -include(“student.hrl”). BestStudent = #student{}. SeniorStudent = #student{class=senior}. AnotherSenior = BestStudent#student{age=17}. #student{age=Age} = AnotherSenior. Age. %% Will print 17
  • 31. Programming Erlang: the Sequential Part 31 ExamplesExamples clear_status (#todo{status=S,who=W})=R) → R#todo{status=finished}.
  • 32. Programming Erlang: the Sequential Part 32 Conditional statementsConditional statements case Expression of Pattern1 [when Guard1] → seq1; Pattern2 [when Guard2] → seq2; end if Guard1 → sequence1; Guard2 → sequence2; end If there is no match, an exception is raised. Atom true matches everything.
  • 33. Programming Erlang: the Sequential Part 33 ExamplesExamples %% Split a lists into odd and even elements odds_and_evens (L) → odds_and_evens (L,[],[]). odds_and_evens ([H|T],Odds,Evens) → case (H rem 2) of 1 → odds_and_evens (T,[H|Odds],Evens); 0 → odds_and_evens (T, Odds, [H, Evens]) end odds_and_evens ([], Odds, Evens) → {lists:reverse (Odds), lists:reverse (Evens)}.
  • 34. Programming Erlang: the Sequential Part 34 ● There are three types of errors in Erlang: ● exit(Why) broadcasts {'EXIT',Pid,Why} to all linked processes ● throw(Why) throws an exception ● erlang:error(Why) terminates the current process immediately Raising ExceptionsRaising Exceptions
  • 35. Programming Erlang: the Sequential Part 35 Catching an ExceptionCatching an Exception try FuncOrExprSeq %% This part can be omitted of Pattern1 [when Guard1] → Expression1; ... catch %% Type can be throw (default), exit, error or _:_ ExType1: ExPattern1 [when ExGuard1] → ExExpr1; ... after %% Guaranteed to execute! AfterExpression end
  • 36. Programming Erlang: the Sequential Part 36 ExampleExample try math:sqrt(-1) catch error: {badarith, _} → .... end
  • 37. Programming Erlang: the Sequential Part 37 Process DictionaryProcess Dictionary ● Each process has a private dictionary: an associative array ● Built-in functions (BIFs) to work with dictionaries: ● @spec put (Key, Value) → OldValue. ● @spec get (Key) → Value | undefined. ● @spec get_keys () → [Key]. ● @spec get () → [{Key, Value}]. ● @spec erase (Key) → Value. ● @spec erase() → [{Key, Value}].
  • 38. Programming Erlang: the Sequential Part 38 MacrosMacros ● Defined using -define(). attribute. ● Called using ?. ● Parameters allowed. ● Predefined macros: ?FILE, ?MODULE, ?LINE. ● Other attributes: -ifdef()., -undef()., -ifndef()., -else., -endif.
  • 39. Programming Erlang: the Sequential Part 39 ExamplesExamples %% To be used in binaries -define (DWORD, 32/unsigned-little-integer). ?DWORD. %% Simple macro with parameters -ifndef (isodd). -define (isodd (X), X rem 2 =:= 1). -endif. ?isodd (77).
  • 40. Programming Erlang: the Sequential Part 40 This presentation roughly covers the first 27 pages (of the total of 29 ) of “Programming Erlang. Software for a Concurrent World,” by Joe Armstrong.