SlideShare une entreprise Scribd logo
1  sur  19
Erlang – Quick Intro Brought to you by Raymond Tay
Erlang? Small language designed more than 20 years ago by Joe Armstrong, Robert Virding and Mike Williams of Ericsson’s CS Lab.  Functional Language. Lightweight concurrency model (aka Actors). Yes, Scalahas a Actors model where they share similar syntax and machinery. Great for soft real-time systems. E.g. Second Life’s chat service Not good for number crunching. There are better tools for that e.g. SciPyNumPy
Yah yah yah…WHO actually uses Erlang? Amazon uses Erlang to implement SimpleDB as part of its EC2 services Facebook uses Erlang to power its backend chat service for its 100 million active users Ericsson uses Erlang to support its GPRS and 3G mobile networks T-Mobile uses Erlang in its SMS and authentication systems
So, what are the benefits of Erlang? Easy to learn, easy to use. Expressing distributing computing is easier Garbage collection is implemented on a per process basis An process in Erlang is light-weight i.e. not a OS process. Each pair of processes communicate by passing messages Designed to operate behind firewalls. Think private compute cloud
Let’s learn how Erlang does it Numbers Variables (they’re not variable) Atoms Boolean Algebra and Comparison Operators Tuples Lists (and comprehensions) Modules in Erlang
There’s more… High-order functions and Lambdas Errors and catching them (We don’t have time for this) Concurrency in Erlang and Actors etc (We don’t have time for this too) Finally we’ll end with a demonstration
Working with Erlang In FP, statements do not exist only expressions. Expressions always return a result and more often than not, have no side effects at all; statements (depending on the lang) may / may not return a result but always have side effects. Expressions, in Erlang, have to be terminated with a period followed by whitespace (,) E.g expression involving numbers 2 + 5. 5 / 2. (same as 5 div 2.) 5 rem 2.  -50 * (100 – 499). (Erlang understands the typical math operator precedences) 2#101 (same as the decimal number 5 only expressed in binary i.e. base 2) 16#cafebabe (do you know what this magic number is expressed in base 16?)
Variables in Erlang (seriously???) No such thing as a variable variable In Erlang, creating a variable and associating with a value is termed bounding. You cannot change the value once bounded. One = 1. One = 2. (Will not succeed) Two = 2. Two = One + One. (Will succeed) _ = 999999999 (Will always succeed. ‘_’ is the i-don’t-care-variable) The ‘=‘ acts as an assignment and pattern matching operator. Pattern matching occurs with/without the ‘=‘. More later.
Atoms (Like the physics term but not like it) In physics of the 1920s, atoms were the smallest things known to man. Today, there are units smaller than the atom like hadrons, quarks. In Erlang, an atom is a literal. hello. (Typing Hello. means to extract the value of Hello) ‘Hello there’. (anything in quotes is an atom) ‘_this_is_a_#really_!long_@string’.
Boolean Algebra, Operators In Erlang, the atom true is Truth whereas false is Falsehood. You can use =:= and =/= for comparing equality and inquality You can use == and /= for comparing equality and inequality if you don’t care about exactness / precisions 5 =:= 5. (will return true) 5 == 5. (will return true) 5 =:= 5.0. (will return false) 5 == 5.0. (will return true)
Tuples They allow you to aggregate values to form some sort of data structure. X = 10, Y = 4. Point = {X, Y}. (Tuple is created) {MyX, _} = Point. (Pattern matching at play. MyX will get the value 10) {_, _, _} = Point. (will fail and erlang will complain RHS don’t match LHS)
Lists In FP, lists are completely indispensable. Same here You can create lists, obviously. You can concat, intersect them. You can retrieve the head or tail of a list via ‘hd’ and ‘tl’ commands in the ‘erlang’ module MyList = [1,2,3,4,5]. MyList ++ [7,8] - - [1,2,3,4,5,6,78]. (equivalent to (MyList ++ ([7,8] - - [1,2,3,4,5,6,7,8])) ) hd( [1,2] ). (will return 1 and take note its not a list) tl([1,2]). (will return [2] which is a list)
List Comprehension In other languages, its called a for-comprehension. Let’s look at code examples [2 * N | | N <- [1,2,3]]. (returns [2,4,6]) [X | | X <- [1,2,3,4,5,6], X rem 2 =:= 0]. (returns [2,4,6] ) Let’s look at a more complex example (taken from sg.finance.yahoo.com) [ {Quote,Price} | | {Quote, Price} <- [ {e3s, 0.4}, {g13.si, 1.66}, {'5im.si',0.22}, {e5h.si, 0.64} ], Price > 0.5]. (returns [{'g13.si',1.66},{'e5h.si',0.64}]) Nested-for-loops (Ugly word in Erlang, but u get the meaning) [ {X, Y} | | X <- [1,2], Y <- [3,4]]. (returns [{1,3}, {1,4}, {2,3}, {2,4}])
How to avoid clustering all code in a single file? You need modules. You need to declare your module’s name, attributes, what functions are exported
Compiling and using your modules There are a couple of methods to do this but for the sake of brevity, here’s the simplest
Functions and Lambdas Functions are easy to write in Erlang. You don’t need to say ‘def’, ‘function’ or give any access specifier like ‘public’, ‘protected’ any of that nonsense. Here’s an example of a Mapper function. map(_, []) -> []; map(F, [H|T]) -> [F(H) | map(F, T)]. Another example of a Folder function fold(F, Start, []) -> Start; fold(F, Start, [H|T]) -> fold(F, F(H, Start), T). A lambda function is simply a nameless function and sometimes called an anonymous function. They can do pretty much anything except recursion. The form is like this: fun (Args1) -> Expression1, … , Expression N ;        (Args2) -> Expression1, … , Expression N; end.
Lambdas
More Lambdas…
Demo This is a demo of a sorting algorithm I wrote in 2007 as it illustrates most of the concepts we talked about.

Contenu connexe

Tendances

Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
dplunkett
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
dplunkett
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
dplunkett
 

Tendances (20)

Applicative Functor - Part 2
Applicative Functor - Part 2Applicative Functor - Part 2
Applicative Functor - Part 2
 
Functional Effects - Part 1
Functional Effects - Part 1Functional Effects - Part 1
Functional Effects - Part 1
 
Q2
Q2Q2
Q2
 
Anfis (1)
Anfis (1)Anfis (1)
Anfis (1)
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5
 
Database management system session 5
Database management system session 5Database management system session 5
Database management system session 5
 
‘go-to’ general-purpose sequential collections - from Java To Scala
‘go-to’ general-purpose sequential collections -from Java To Scala‘go-to’ general-purpose sequential collections -from Java To Scala
‘go-to’ general-purpose sequential collections - from Java To Scala
 
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
Scala 3 by Example - Algebraic Data Types for Domain Driven Design - Part 1
 
The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)The Functional Programmer's Toolkit (NDC London 2019)
The Functional Programmer's Toolkit (NDC London 2019)
 
The swift programming language
The swift programming languageThe swift programming language
The swift programming language
 
Ap Power Point Chpt3 B
Ap Power Point Chpt3 BAp Power Point Chpt3 B
Ap Power Point Chpt3 B
 
The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)The Power Of Composition (DotNext 2019)
The Power Of Composition (DotNext 2019)
 
Ap Power Point Chpt2
Ap Power Point Chpt2Ap Power Point Chpt2
Ap Power Point Chpt2
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
 
Clean code lecture part I
Clean code lecture part IClean code lecture part I
Clean code lecture part I
 
Ap Power Point Chpt3
Ap Power Point Chpt3Ap Power Point Chpt3
Ap Power Point Chpt3
 
Compositionality and Category Theory - a montage of slides/transcript for sec...
Compositionality and Category Theory - a montage of slides/transcript for sec...Compositionality and Category Theory - a montage of slides/transcript for sec...
Compositionality and Category Theory - a montage of slides/transcript for sec...
 
Regular expression for dfa
Regular expression for dfaRegular expression for dfa
Regular expression for dfa
 
DBMS CS3
DBMS CS3DBMS CS3
DBMS CS3
 
The Power of Composition
The Power of CompositionThe Power of Composition
The Power of Composition
 

En vedette

Basic definitions of queuing theory seminar
Basic definitions of queuing theory seminarBasic definitions of queuing theory seminar
Basic definitions of queuing theory seminar
Trisha Gopalakrishna
 
QUEUING THEORY
QUEUING THEORYQUEUING THEORY
QUEUING THEORY
avtarsingh
 

En vedette (14)

Basic definitions of queuing theory seminar
Basic definitions of queuing theory seminarBasic definitions of queuing theory seminar
Basic definitions of queuing theory seminar
 
Queuing Theory - Operation Research
Queuing Theory - Operation ResearchQueuing Theory - Operation Research
Queuing Theory - Operation Research
 
Queueing theory
Queueing theoryQueueing theory
Queueing theory
 
QUEUING THEORY
QUEUING THEORYQUEUING THEORY
QUEUING THEORY
 
Why erlang
Why erlangWhy erlang
Why erlang
 
Mobile phones
Mobile phonesMobile phones
Mobile phones
 
Erlang: Software for a Concurrent world
Erlang: Software for a Concurrent worldErlang: Software for a Concurrent world
Erlang: Software for a Concurrent world
 
Intro To Erlang
Intro To ErlangIntro To Erlang
Intro To Erlang
 
Intro to Erlang
Intro to ErlangIntro to Erlang
Intro to Erlang
 
Queuing problems
Queuing problemsQueuing problems
Queuing problems
 
Queueing theory
Queueing theoryQueueing theory
Queueing theory
 
QUEUING THEORY
QUEUING THEORY QUEUING THEORY
QUEUING THEORY
 
Queuing theory
Queuing theoryQueuing theory
Queuing theory
 
Queueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS ApplicationsQueueing Theory and its BusinessS Applications
Queueing Theory and its BusinessS Applications
 

Similaire à Introduction to Erlang

Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
Antony Stubbs
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Raghu nath
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
priestmanmable
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
hccit
 

Similaire à Introduction to Erlang (20)

Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
 
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...
 
Erlang, an overview
Erlang, an overviewErlang, an overview
Erlang, an overview
 
Scala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love GameScala Language Intro - Inspired by the Love Game
Scala Language Intro - Inspired by the Love Game
 
Erlang session1
Erlang session1Erlang session1
Erlang session1
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
外傷的Elixir
外傷的Elixir外傷的Elixir
外傷的Elixir
 
Module 2 topic 1 notes
Module 2 topic 1 notesModule 2 topic 1 notes
Module 2 topic 1 notes
 
java_lect_03-2.ppt
java_lect_03-2.pptjava_lect_03-2.ppt
java_lect_03-2.ppt
 
Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1
 
3.5
3.53.5
3.5
 
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docxISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
ISTA 130 Lab 21 Turtle ReviewHere are all of the turt.docx
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
Introduction To Rust part II Presentation
Introduction To Rust part II PresentationIntroduction To Rust part II Presentation
Introduction To Rust part II Presentation
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
Maxbox starter20
Maxbox starter20Maxbox starter20
Maxbox starter20
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Hub102 - JS - Lesson3
Hub102 - JS - Lesson3Hub102 - JS - Lesson3
Hub102 - JS - Lesson3
 
Elixir
ElixirElixir
Elixir
 
5 structured programming
5 structured programming 5 structured programming
5 structured programming
 

Plus de Raymond Tay

Plus de Raymond Tay (8)

Principled io in_scala_2019_distribution
Principled io in_scala_2019_distributionPrincipled io in_scala_2019_distribution
Principled io in_scala_2019_distribution
 
Building a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beamBuilding a modern data platform with scala, akka, apache beam
Building a modern data platform with scala, akka, apache beam
 
Practical cats
Practical catsPractical cats
Practical cats
 
Toying with spark
Toying with sparkToying with spark
Toying with spark
 
Distributed computing for new bloods
Distributed computing for new bloodsDistributed computing for new bloods
Distributed computing for new bloods
 
Functional programming with_scala
Functional programming with_scalaFunctional programming with_scala
Functional programming with_scala
 
Introduction to cuda geek camp singapore 2011
Introduction to cuda   geek camp singapore 2011Introduction to cuda   geek camp singapore 2011
Introduction to cuda geek camp singapore 2011
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
 

Dernier

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
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...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

Introduction to Erlang

  • 1. Erlang – Quick Intro Brought to you by Raymond Tay
  • 2. Erlang? Small language designed more than 20 years ago by Joe Armstrong, Robert Virding and Mike Williams of Ericsson’s CS Lab. Functional Language. Lightweight concurrency model (aka Actors). Yes, Scalahas a Actors model where they share similar syntax and machinery. Great for soft real-time systems. E.g. Second Life’s chat service Not good for number crunching. There are better tools for that e.g. SciPyNumPy
  • 3. Yah yah yah…WHO actually uses Erlang? Amazon uses Erlang to implement SimpleDB as part of its EC2 services Facebook uses Erlang to power its backend chat service for its 100 million active users Ericsson uses Erlang to support its GPRS and 3G mobile networks T-Mobile uses Erlang in its SMS and authentication systems
  • 4. So, what are the benefits of Erlang? Easy to learn, easy to use. Expressing distributing computing is easier Garbage collection is implemented on a per process basis An process in Erlang is light-weight i.e. not a OS process. Each pair of processes communicate by passing messages Designed to operate behind firewalls. Think private compute cloud
  • 5. Let’s learn how Erlang does it Numbers Variables (they’re not variable) Atoms Boolean Algebra and Comparison Operators Tuples Lists (and comprehensions) Modules in Erlang
  • 6. There’s more… High-order functions and Lambdas Errors and catching them (We don’t have time for this) Concurrency in Erlang and Actors etc (We don’t have time for this too) Finally we’ll end with a demonstration
  • 7. Working with Erlang In FP, statements do not exist only expressions. Expressions always return a result and more often than not, have no side effects at all; statements (depending on the lang) may / may not return a result but always have side effects. Expressions, in Erlang, have to be terminated with a period followed by whitespace (,) E.g expression involving numbers 2 + 5. 5 / 2. (same as 5 div 2.) 5 rem 2. -50 * (100 – 499). (Erlang understands the typical math operator precedences) 2#101 (same as the decimal number 5 only expressed in binary i.e. base 2) 16#cafebabe (do you know what this magic number is expressed in base 16?)
  • 8. Variables in Erlang (seriously???) No such thing as a variable variable In Erlang, creating a variable and associating with a value is termed bounding. You cannot change the value once bounded. One = 1. One = 2. (Will not succeed) Two = 2. Two = One + One. (Will succeed) _ = 999999999 (Will always succeed. ‘_’ is the i-don’t-care-variable) The ‘=‘ acts as an assignment and pattern matching operator. Pattern matching occurs with/without the ‘=‘. More later.
  • 9. Atoms (Like the physics term but not like it) In physics of the 1920s, atoms were the smallest things known to man. Today, there are units smaller than the atom like hadrons, quarks. In Erlang, an atom is a literal. hello. (Typing Hello. means to extract the value of Hello) ‘Hello there’. (anything in quotes is an atom) ‘_this_is_a_#really_!long_@string’.
  • 10. Boolean Algebra, Operators In Erlang, the atom true is Truth whereas false is Falsehood. You can use =:= and =/= for comparing equality and inquality You can use == and /= for comparing equality and inequality if you don’t care about exactness / precisions 5 =:= 5. (will return true) 5 == 5. (will return true) 5 =:= 5.0. (will return false) 5 == 5.0. (will return true)
  • 11. Tuples They allow you to aggregate values to form some sort of data structure. X = 10, Y = 4. Point = {X, Y}. (Tuple is created) {MyX, _} = Point. (Pattern matching at play. MyX will get the value 10) {_, _, _} = Point. (will fail and erlang will complain RHS don’t match LHS)
  • 12. Lists In FP, lists are completely indispensable. Same here You can create lists, obviously. You can concat, intersect them. You can retrieve the head or tail of a list via ‘hd’ and ‘tl’ commands in the ‘erlang’ module MyList = [1,2,3,4,5]. MyList ++ [7,8] - - [1,2,3,4,5,6,78]. (equivalent to (MyList ++ ([7,8] - - [1,2,3,4,5,6,7,8])) ) hd( [1,2] ). (will return 1 and take note its not a list) tl([1,2]). (will return [2] which is a list)
  • 13. List Comprehension In other languages, its called a for-comprehension. Let’s look at code examples [2 * N | | N <- [1,2,3]]. (returns [2,4,6]) [X | | X <- [1,2,3,4,5,6], X rem 2 =:= 0]. (returns [2,4,6] ) Let’s look at a more complex example (taken from sg.finance.yahoo.com) [ {Quote,Price} | | {Quote, Price} <- [ {e3s, 0.4}, {g13.si, 1.66}, {'5im.si',0.22}, {e5h.si, 0.64} ], Price > 0.5]. (returns [{'g13.si',1.66},{'e5h.si',0.64}]) Nested-for-loops (Ugly word in Erlang, but u get the meaning) [ {X, Y} | | X <- [1,2], Y <- [3,4]]. (returns [{1,3}, {1,4}, {2,3}, {2,4}])
  • 14. How to avoid clustering all code in a single file? You need modules. You need to declare your module’s name, attributes, what functions are exported
  • 15. Compiling and using your modules There are a couple of methods to do this but for the sake of brevity, here’s the simplest
  • 16. Functions and Lambdas Functions are easy to write in Erlang. You don’t need to say ‘def’, ‘function’ or give any access specifier like ‘public’, ‘protected’ any of that nonsense. Here’s an example of a Mapper function. map(_, []) -> []; map(F, [H|T]) -> [F(H) | map(F, T)]. Another example of a Folder function fold(F, Start, []) -> Start; fold(F, Start, [H|T]) -> fold(F, F(H, Start), T). A lambda function is simply a nameless function and sometimes called an anonymous function. They can do pretty much anything except recursion. The form is like this: fun (Args1) -> Expression1, … , Expression N ; (Args2) -> Expression1, … , Expression N; end.
  • 19. Demo This is a demo of a sorting algorithm I wrote in 2007 as it illustrates most of the concepts we talked about.