SlideShare a Scribd company logo
1 of 58
Download to read offline
Introduction to Erlang
Erlang is a functional, concurrency-oriented,
distributive, fault-tolerant programming
language.




                                                1
Philosophy of Erlang



 “Erlang was designed for writing concurrent programs
 that quot;run foreverquot;.” - Joe Armstrong
 Let it fail. Have another process deal with it.
 Fault tolerance requires at least *two* computers.




                                                        2
Erlang Mindset

Lots of little tasks
Tons of processes
Each process does a task very well
Ant Colony
Ants are processes sending messages to each other




                                                    3
Why Erlang?




              4
Erlang makes hard things
         easier.



                           5
Concurrency




              6
7
Spawning Processes




                     8
F = fun() ->
      io:format(“hi”)
   end,
spawn(F).

>> hi<0.34.0>




                        9
spawn(io, format, [“hi”]).




                             10
Message Passing




                  11
12
Pid ! Message




Pid ! {do_task, run_home}




Pid ! 2


                            13
receive ... end



receive
 Pattern1 [when Guard1] ->
   Expressions1;
 Pattern2 [when Guard2] ->
   Expressions2
end.



                             14
Messages are contained in a process’s
               inbox.

receive
 _ ->
   ok
end.


                                         15
Fault Tolerance




                  16
17
18
which is why erlang was
   designed to be....



                          19
Distributive




               20
21
Pid ! Message



     where Pid exists on another node.




                                         22
Erlang Nodes
Can only talk if the erlang ‘cookie’ is the same
Can exist on the same machine or different machines
Does have overhead, isn’t free
Can have ‘C’ nodes (+ other node types that speak
erlang)
Communicate via Global Name Registry, Specific Pids,
or by Node name



                                                      23
The Basics




             24
Erlang is Functional




                       25
Periods, Commas,
Semicolons Oh My!
People think Erlang’s syntax is ugly. It’s actually
very logical.




                                                      26
Periods ( . ) end everything except when

Semicolons ( ; ) end clauses

and Commas ( , ) separate expressions.




                                           27
calculate_area({square, Size}) ->
  Size * Size;

calculate_area({circle, Radius}) ->
  Pi = 3.14,
  Pi * Radius * Radius;

calculate_area(_Unknown) ->
  ok.




                                      28
case Expression of
 {do_task, run_home} ->
   Home = 123,
   spawn(run, home, [Home]);
 _ ->
   ok
end.




                               29
Data Types




             30
Integer - however big you can imagine

Floats

Atoms - similar to a ruby symbol, they are used to
represent non-numerical constants (true, false, ok,
error)

Tuple - Fixed length collection of items ({one, two,
three} or {one, two} etc)

Lists - Stores a variable number of things ([1, 2, {one,
two}, $a])


                                                           31
Missing Links...




                   32
Strings - A string is just a list of integers in erlang.

Booleans - Use atoms (true, false)

Hash - Property List ( [tuple()] ) shown later




                                                           33
Pattern Matching
Once you pattern match, variable assigning feels dirty.




                                                          34
1 = 1    ( ok )



1 = 2    ( error thrown bad_match )



Value = {2, 4},
{Width, Height} = {2, 4}   ( ok )


[Head|Tail] = [1,2,3,4]     ( ok )



                                      35
Variable Binding
or restricting infinite possibilities.




                                        36
An unbound variable can be anything it
    needs to be (infinite possibilities)

A.




                                          37
Pattern matching an unbound variable is
       like restricting what it can be.
A = 1.


A = 2.   (error bad_match)




                                           38
Tuples
Single entity that holds a collection of items.




                                                  39
{1, 2, 3}.



{one, two}.



{user_id, 2, user_name, “asceth”}.




                                     40
Lists
Variable sized container of items.




                                     41
List = [1, 2, 3],
NewList = List ++ [4, 5, 6].
>> [1, 2, 3, 4, 5, 6]




[Head|Tail] = NewList.
>> Head -> 1
>> Tail -> [2, 3, 4, 5, 6]




                               42
[tuple()]
Property Lists or semi-hashes.




                                 43
PList = [{key, value}, {key1, value1}].




case lists:keysearch(key, 1, PList) of
 false ->
   error;
 {value, {Key, Value}} ->
   Value
end.



                                          44
keydelete - deletes tuple with specified key in list of
tuples and returns new list

keymap - returns list of tuples where Nth element is
replaced be specified fun

keymember - tests for existence of key

keymerge - combines two tuples with tuple1 taking
priority

ukeymerge, ukeysort - like keymerge but removes
duplicates in tuple2


                                                         45
Functions
Pattern matching, Function Arities & more




                                            46
Functions with the same name can exist

Functions with the same name and different arity can
exist

Erlang tells you if a function cannot be called due to
order of definement

Function order is very important




                                                         47
contains(Key, []) ->
  false;
contains(Key, List) ->
  lists:any(fun(X) -> Key == X end, List).




contains(1, [1, 2, 3]).
true


contains({home, 123}, [{apartment, 333}, {house, 000}]).
false




                                                           48
Anonymous




            49
contains(Key, []) ->
  false;
contains(Key, List) ->
  lists:any(fun(X) -> Key == X end, List).




F = fun(X, Y) ->
        X1 = X * 2,
        Y1 = Y * X,
        X1 + Y1
     end,
F(2, 2).
>> 12




                                             50
Looping
Tail recursion makes looping good again.




                                           51
print([]) ->
  ok;


print([H|T]) ->
  io:format(“~p~n”, [H]),
  print(T).




                            52
Conditionals
If’s are okay, cases are cooler.




                                   53
if
  A == 5 ->
    do_something();
  true ->
    do_something_else()
end.




                          54
case A of
 0 ->
   first();
 2 ->
   second();
 _ ->
   something()
end.



                 55
Resources
http://www.erlang.org
Technorati




                        56
57
Questions?




             58

More Related Content

What's hot

Functions in python
Functions in pythonFunctions in python
Functions in python
Ilian Iliev
 

What's hot (20)

Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part ...
 
non-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parametersnon-strict functions, bottom and scala by-name parameters
non-strict functions, bottom and scala by-name parameters
 
16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues16. Arrays Lists Stacks Queues
16. Arrays Lists Stacks Queues
 
16. Java stacks and queues
16. Java stacks and queues16. Java stacks and queues
16. Java stacks and queues
 
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for Fun and Profit – Haskell and...
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala - Part 2
 
Lezione03
Lezione03Lezione03
Lezione03
 
15. Streams Files and Directories
15. Streams Files and Directories 15. Streams Files and Directories
15. Streams Files and Directories
 
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and ScalaFolding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
Folding Unfolded - Polyglot FP for Fun and Profit - Haskell and Scala
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 
18. Java associative arrays
18. Java associative arrays18. Java associative arrays
18. Java associative arrays
 
Chapter 2 Java Methods
Chapter 2 Java MethodsChapter 2 Java Methods
Chapter 2 Java Methods
 
1-D array
1-D array1-D array
1-D array
 
Searching/Sorting algorithms
Searching/Sorting algorithmsSearching/Sorting algorithms
Searching/Sorting algorithms
 
Functions in python
Functions in pythonFunctions in python
Functions in python
 
Practical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan HodorogPractical Functional Programming Presentation by Bogdan Hodorog
Practical Functional Programming Presentation by Bogdan Hodorog
 
Haskell
HaskellHaskell
Haskell
 
Chapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQChapter 22. Lambda Expressions and LINQ
Chapter 22. Lambda Expressions and LINQ
 
Haskell for data science
Haskell for data scienceHaskell for data science
Haskell for data science
 
Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)Python Functions (PyAtl Beginners Night)
Python Functions (PyAtl Beginners Night)
 

Viewers also liked

Distributed Erlang Systems In Operation
Distributed Erlang Systems In OperationDistributed Erlang Systems In Operation
Distributed Erlang Systems In Operation
Andy Gross
 
東京Node学園#8 Let It Crash!?
東京Node学園#8 Let It Crash!?東京Node学園#8 Let It Crash!?
東京Node学園#8 Let It Crash!?
koichik
 
Erlang vs. Java
Erlang vs. JavaErlang vs. Java
Erlang vs. Java
Artan Cami
 
Lightning Talk: Erlang on Xen - Mikhail Bortnyk
Lightning Talk: Erlang on Xen - Mikhail BortnykLightning Talk: Erlang on Xen - Mikhail Bortnyk
Lightning Talk: Erlang on Xen - Mikhail Bortnyk
Elixir Club
 
The mystique of erlang
The mystique of erlangThe mystique of erlang
The mystique of erlang
Carob Cherub
 

Viewers also liked (20)

Intro to Erlang
Intro to ErlangIntro to Erlang
Intro to Erlang
 
An introduction to erlang
An introduction to erlangAn introduction to erlang
An introduction to erlang
 
Erlang OTP
Erlang OTPErlang OTP
Erlang OTP
 
Java tips
Java tipsJava tips
Java tips
 
A Brief Introduction To Erlang
A Brief Introduction To ErlangA Brief Introduction To Erlang
A Brief Introduction To Erlang
 
Distributed Erlang Systems In Operation
Distributed Erlang Systems In OperationDistributed Erlang Systems In Operation
Distributed Erlang Systems In Operation
 
東京Node学園#8 Let It Crash!?
東京Node学園#8 Let It Crash!?東京Node学園#8 Let It Crash!?
東京Node学園#8 Let It Crash!?
 
Concurrency in Elixir with OTP
Concurrency in Elixir with OTPConcurrency in Elixir with OTP
Concurrency in Elixir with OTP
 
Erlang vs. Java
Erlang vs. JavaErlang vs. Java
Erlang vs. Java
 
Writing Scalable Software in Java
Writing Scalable Software in JavaWriting Scalable Software in Java
Writing Scalable Software in Java
 
Erlang Concurrency
Erlang ConcurrencyErlang Concurrency
Erlang Concurrency
 
Lightning Talk: Erlang on Xen - Mikhail Bortnyk
Lightning Talk: Erlang on Xen - Mikhail BortnykLightning Talk: Erlang on Xen - Mikhail Bortnyk
Lightning Talk: Erlang on Xen - Mikhail Bortnyk
 
The mystique of erlang
The mystique of erlangThe mystique of erlang
The mystique of erlang
 
Introduction to Couchbase Server 2.0
Introduction to Couchbase Server 2.0Introduction to Couchbase Server 2.0
Introduction to Couchbase Server 2.0
 
Why erlang
Why erlangWhy erlang
Why erlang
 
Erlang on OSv
Erlang on OSvErlang on OSv
Erlang on OSv
 
Erlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The UglyErlang Developments: The Good, The Bad and The Ugly
Erlang Developments: The Good, The Bad and The Ugly
 
Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻Node.jsエンジニア Erlangに入門するの巻
Node.jsエンジニア Erlangに入門するの巻
 
1 hour dive into Erlang/OTP
1 hour dive into Erlang/OTP1 hour dive into Erlang/OTP
1 hour dive into Erlang/OTP
 
Exception Handler, Controller Advice Of Spring
Exception Handler, Controller Advice Of SpringException Handler, Controller Advice Of Spring
Exception Handler, Controller Advice Of Spring
 

Similar to Intro To Erlang

Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
Ross Lawley
 
The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
Norman Richards
 

Similar to Intro To Erlang (20)

Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
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...
 
Thinking Functionally In Ruby
Thinking Functionally In RubyThinking Functionally In Ruby
Thinking Functionally In Ruby
 
Erlang
ErlangErlang
Erlang
 
Functional Concepts for OOP Developers
Functional Concepts for OOP DevelopersFunctional Concepts for OOP Developers
Functional Concepts for OOP Developers
 
Introduction to Erlang
Introduction to ErlangIntroduction to Erlang
Introduction to Erlang
 
7li7w devcon5
7li7w devcon57li7w devcon5
7li7w devcon5
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdf
 
02. haskell motivation
02. haskell motivation02. haskell motivation
02. haskell motivation
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
01. haskell introduction
01. haskell introduction01. haskell introduction
01. haskell introduction
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
Python lecture 05
Python lecture 05Python lecture 05
Python lecture 05
 
The Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unificationThe Logical Burrito - pattern matching, term rewriting and unification
The Logical Burrito - pattern matching, term rewriting and unification
 
Elixir pattern matching and recursion
Elixir pattern matching and recursionElixir pattern matching and recursion
Elixir pattern matching and recursion
 
Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1
 
Elixir basics
Elixir basicsElixir basics
Elixir basics
 
Intro to io
Intro to ioIntro to io
Intro to io
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

Intro To Erlang