SlideShare a Scribd company logo
1 of 36
Download to read offline
Introduction to Functional Programming




                                   Vasilij Savin



               AACIMP 2011
Background: Telecom issues
●   No down time allowed
●   Solution has to be VERY scalable (millions
    operations per second)
●   Maintainable software
●   Distributed systems
History of Erlang
●   Need for robust fault-tolerant language
●   Ericsson did not find one to suit the needs
●   Based originally on Prolog
        –   Inherits many syntax concepts
●   Later rewritten with C for performance
●   Opensource from 2001
Erlang distinguished properties
●   Concurrency
●   Hot code loading
●   Distributed
●   Soft real-time
●   Robust
What Erlang is NOT?
●   Object-oriented
●   C syntax follower
●   Shared memory ideology follower
●   SILVER BULLET
What Erlang is NOT good for?
●   Front-end web development
●   GUI programming
●   Desktop applications
Erlang in the Real World: Companies
●   Klarna (e-commerce)
●   Mobile Arts (telecom)
●   Ericsson (telecom)
●   Goldman Sachs (banking)
●   Facebook (social network)
Erlang in the Real World: Products
●   Ejabberd (instant messanging server)
●   YAWS (webserver)
●   Facebook chat
●   Firmware in Ericsson products
●   Varios telecom services
Paradigm comparison
Imperative (structured,      Functional
procedural)
                             Treats computation as the
Computation in terms of      evaluation of mathematical
statements that directly     functions avoiding state
change a program state       and mutable data.
Object-oriented
Treats datafields as
"objects" manipulated only
through pre-defined
methods
The biggest challenge ahead:
Unlearning what you already know
Basic Concepts
●   Functions are First Class citizens
●   Functions are purely functional
●   Iteration is achieved through recursion
●   Execution Flow control is done with pattern
    matching
Data Types
●   Integers
●   Floats
●   Atoms
●   Lists
●   Tuples
Integers
●   B#Val is used to store         > 16#FF.
    numbers in base < B >
                                   255
●   $Char - ascii value            > $B.
●   Large integers are converted   66
    to bignums
●   Max size depends on physical
    constraints
Floats
●   Calculations are not very        -123.456
    efficient
                                     12.24E-10
●   Stored as double
●   Follow IEEE 754 standard
Atoms
●   Atoms are constant literals       magic_tag
●   Start with a lower case letter    'A quoted tag'
    or are encapsulated by ’ ’        '_can include blanks'
●   Any character code is allowed
    within an atom if using ’ ’
●   Letters integers and _ are
    allowed if the atom starts with
    a lower case letter
Tuples
●   Tuples are used to store a   {123, bcd}
    fixed number of items
                                 {123, def, abc}
●   Tuples of any size are allowed {person, 'Bass', 'Savin'}
●   Must contain valid Erlang      {abc, {def, 12.3}, “jkl”}
    expressions
                                 {}
●   Tuple Arity – number of
    elements in tuple
Lists
●   Used to store a variable           [1,2,'quoted
    number of items                    Atom',atom,"str",123.12,
                                       {tuple,1}]
●   Lists are dynamically sized
                                       > [97,98,99,4,5,6].
●   Strings in Erlang are lists of
    ASCII values                       [97,98,99,4,5,6]

●   Lists are written beginning        > [233].
    with a [ and ending with a ]       "é"
●   Elements are separated by          > [65,66,68].
    commas
                                       "ABD"
Lists II
●   A recursive list definition        “ABC” “DEF” == “ABCDEF”
    consists of a head and a tail
                                       Not recommended
●   Lists whose last Tail term is []   (inefficient)
    are called: proper lists           > [1,2,3,4] – [2,3].
●   Appended automatically             [1,4]

●   Improper lists are of limited      > [1,2,3] ++ [4,5,6].
    use                                [1,2,3,4,5,6]
Variables
●   Variables start with an Upper   A_Variable_name
    Case Letter or _.
                                    AnotherWayForVariable
●   They may not contain any        _do_not_care_variable
    special characters.
                                    _
●    '_' alone is a don’t care
    Variable. Its values are
    ignored and never bound.
●   The value of a variable can
    not be changed once it
    hasbeen bound.
●   Erlang does not have a type
    system.
●   Types are determined at run
    time.
Modules
●   Modules are stored in files with the .erl extension
●   The module and file names must be the same
●   Exported functions can be called from outside the module
●   Use the module prefix when making the call
          –   <module>:<function_name>([arg list]).
●   Local functions can only be called within the module
Module anatomy
Functions
●   Erlang programs consist of functions
●   Functions are defined within Modules
●   Function and module names must be atoms
●   A function is defined as a collection of clauses
●   Variables are pattern matched in the function head. If pattern
    matching fails on a clause, the next one is tested. One clause
    must always succeed
●   Functions return value of the last expression executed
Function anatomy
Pattern Matching:
               <Pattern> = <Expression>



    Pattern Matching is used for:
●   Assigning values to Variables
●   Controlling the execution flow of the programs
●   Extracting values from compound data types
Pattern Matching: Assignment
●   The Pattern can
    containunbound variables
    which are bound when the    A = 10.
    pattern matching succeeds   Str = “nice string”.
●   The Expression may not      List = [1,2,3].
    contain unbound variables
                                Tuple = {abc, List, Str}
                                BadTuple = {List, Data}
                                                          fails
Pattern Matching: Testing
●   A match must either succeed   {A, A, B} = {10, 20,
    or fail                       “str”}
●   Used to pick the execution                           fails
    flow in:                      [A, B] = [1, 2, 3, 4]
         –   case statements                             fails
         –   receive statements   [A, B | C] = [1,2,3,4,5]
         –   function heads                       succeeds
                                  A = 1, B = 2, C = [3,4,5]
Pattern Matching: Value Extraction
●   Used to extract values from complex data types
●   Note the use of _, the don't care variable



{_, Name, {Street, City, _}} =
{person, “Vasilij”, {“Kantatvagen”, 'Stockholm',
sweden}}
Case Anatomy
Case statement
●   One branch must always succeed
●   By putting the ‘_’ or an unbound variable in the
    last clause ensures that the clause will always be
    picked should the previous ones not match
●   The _ clause is not mandatory
Guards
●   Guards are additional clauses that can go in a function's head to
    make pattern matching more expressive.
●   All variables in guards have to be bound
●   If all guards have to succeed, use , to separate them
●   If one guard has to succeed, use ; to separate them
●   Guards have to be free of side effects
●   There are restrictions on BIFS and expressions in guards
●   User functions are not allowed in guards

foo(String) when is_list(String), length(String) < 10 →
...
foo(Tuple) when is_tuple(Tuple);is_atom(Tuple) → ...
Recursion examples
odd([Head|Tail]) when Head rem 1 == 0 ->
   [Head|even(Tail)];
odd([_Head|Tail]) ->
   even(Tail);
odd([]) ->
   [].
Recursion with accumulators
average(X) -> average(X, 0, 0).


average([H|T], Length, Sum) ->
   average(T, Length + 1, Sum + H);
average([], Length, Sum) ->
   Sum / Length.
Runtime Errors
●   badarg – BIF with wrong arguments is called
●   badmatch – patternmatching failed and clause list
    is exhausted
●   function clause – no function clause matched the
    pattern
Useful libraries
●   io.erl – general I/O functionality
●   file.erl – general filesystem functionality
●   lists.erl – helpful list function
                  More documentation can be found here:
                          http://www.erlang.org/doc/

More Related Content

What's hot

Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and OperatorsMarwa Ali Eissa
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in ScalaAyush Mishra
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7sumitbardhan
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in javaShashwat Shriparv
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPKamal Acharya
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in JavaMudit Gupta
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2sumitbardhan
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3sumitbardhan
 

What's hot (13)

Python ppt
Python pptPython ppt
Python ppt
 
Data Types, Variables, and Operators
Data Types, Variables, and OperatorsData Types, Variables, and Operators
Data Types, Variables, and Operators
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Introducing Pattern Matching in Scala
 Introducing Pattern Matching  in Scala Introducing Pattern Matching  in Scala
Introducing Pattern Matching in Scala
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Road to code
Road to codeRoad to code
Road to code
 
Fundamental programming structures in java
Fundamental programming structures in javaFundamental programming structures in java
Fundamental programming structures in java
 
Text and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHPText and Numbers (Data Types)in PHP
Text and Numbers (Data Types)in PHP
 
Xml session
Xml sessionXml session
Xml session
 
Best practices in Java
Best practices in JavaBest practices in Java
Best practices in Java
 
358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2358 33 powerpoint-slides_2-functions_chapter-2
358 33 powerpoint-slides_2-functions_chapter-2
 
358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3358 33 powerpoint-slides_3-pointers_chapter-3
358 33 powerpoint-slides_3-pointers_chapter-3
 

Similar to Erlang intro

Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1Dmitry Zinoviev
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2Chris Farrell
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Quick python reference
Quick python referenceQuick python reference
Quick python referenceJayant Parida
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_reviewEdureka!
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1Mukesh Kumar
 
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
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoreambikavenkatesh2
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 CertificationsGiacomo Veneri
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstartRyan Brown
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objectsHarkamal Singh
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersMiles Sabin
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersSkills Matter
 

Similar to Erlang intro (20)

Introduction to Erlang Part 1
Introduction to Erlang Part 1Introduction to Erlang Part 1
Introduction to Erlang Part 1
 
Java best practices
Java best practicesJava best practices
Java best practices
 
JavaScript: Patterns, Part 2
JavaScript: Patterns, Part  2JavaScript: Patterns, Part  2
JavaScript: Patterns, Part 2
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Quick python reference
Quick python referenceQuick python reference
Quick python reference
 
C language
C languageC language
C language
 
Android webinar class_java_review
Android webinar class_java_reviewAndroid webinar class_java_review
Android webinar class_java_review
 
Programming in scala - 1
Programming in scala - 1Programming in scala - 1
Programming in scala - 1
 
C Programming - Refresher - Part II
C Programming - Refresher - Part II C Programming - Refresher - Part II
C Programming - Refresher - Part II
 
kotlin-nutshell.pptx
kotlin-nutshell.pptxkotlin-nutshell.pptx
kotlin-nutshell.pptx
 
Erlang
ErlangErlang
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...
 
data structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysoredata structures using C 2 sem BCA univeristy of mysore
data structures using C 2 sem BCA univeristy of mysore
 
Session 4
Session 4Session 4
Session 4
 
Preparing Java 7 Certifications
Preparing Java 7 CertificationsPreparing Java 7 Certifications
Preparing Java 7 Certifications
 
Erlang kickstart
Erlang kickstartErlang kickstart
Erlang kickstart
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
 
A Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java DevelopersA Brief Introduction to Scala for Java Developers
A Brief Introduction to Scala for Java Developers
 
Miles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java DevelopersMiles Sabin Introduction To Scala For Java Developers
Miles Sabin Introduction To Scala For Java Developers
 
Cs3430 lecture 15
Cs3430 lecture 15Cs3430 lecture 15
Cs3430 lecture 15
 

More from SSA KPI

Germany presentation
Germany presentationGermany presentation
Germany presentationSSA KPI
 
Grand challenges in energy
Grand challenges in energyGrand challenges in energy
Grand challenges in energySSA KPI
 
Engineering role in sustainability
Engineering role in sustainabilityEngineering role in sustainability
Engineering role in sustainabilitySSA KPI
 
Consensus and interaction on a long term strategy for sustainable development
Consensus and interaction on a long term strategy for sustainable developmentConsensus and interaction on a long term strategy for sustainable development
Consensus and interaction on a long term strategy for sustainable developmentSSA KPI
 
Competences in sustainability in engineering education
Competences in sustainability in engineering educationCompetences in sustainability in engineering education
Competences in sustainability in engineering educationSSA KPI
 
Introducatio SD for enginers
Introducatio SD for enginersIntroducatio SD for enginers
Introducatio SD for enginersSSA KPI
 
DAAD-10.11.2011
DAAD-10.11.2011DAAD-10.11.2011
DAAD-10.11.2011SSA KPI
 
Talking with money
Talking with moneyTalking with money
Talking with moneySSA KPI
 
'Green' startup investment
'Green' startup investment'Green' startup investment
'Green' startup investmentSSA KPI
 
From Huygens odd sympathy to the energy Huygens' extraction from the sea waves
From Huygens odd sympathy to the energy Huygens' extraction from the sea wavesFrom Huygens odd sympathy to the energy Huygens' extraction from the sea waves
From Huygens odd sympathy to the energy Huygens' extraction from the sea wavesSSA KPI
 
Dynamics of dice games
Dynamics of dice gamesDynamics of dice games
Dynamics of dice gamesSSA KPI
 
Energy Security Costs
Energy Security CostsEnergy Security Costs
Energy Security CostsSSA KPI
 
Naturally Occurring Radioactivity (NOR) in natural and anthropic environments
Naturally Occurring Radioactivity (NOR) in natural and anthropic environmentsNaturally Occurring Radioactivity (NOR) in natural and anthropic environments
Naturally Occurring Radioactivity (NOR) in natural and anthropic environmentsSSA KPI
 
Advanced energy technology for sustainable development. Part 5
Advanced energy technology for sustainable development. Part 5Advanced energy technology for sustainable development. Part 5
Advanced energy technology for sustainable development. Part 5SSA KPI
 
Advanced energy technology for sustainable development. Part 4
Advanced energy technology for sustainable development. Part 4Advanced energy technology for sustainable development. Part 4
Advanced energy technology for sustainable development. Part 4SSA KPI
 
Advanced energy technology for sustainable development. Part 3
Advanced energy technology for sustainable development. Part 3Advanced energy technology for sustainable development. Part 3
Advanced energy technology for sustainable development. Part 3SSA KPI
 
Advanced energy technology for sustainable development. Part 2
Advanced energy technology for sustainable development. Part 2Advanced energy technology for sustainable development. Part 2
Advanced energy technology for sustainable development. Part 2SSA KPI
 
Advanced energy technology for sustainable development. Part 1
Advanced energy technology for sustainable development. Part 1Advanced energy technology for sustainable development. Part 1
Advanced energy technology for sustainable development. Part 1SSA KPI
 
Fluorescent proteins in current biology
Fluorescent proteins in current biologyFluorescent proteins in current biology
Fluorescent proteins in current biologySSA KPI
 
Neurotransmitter systems of the brain and their functions
Neurotransmitter systems of the brain and their functionsNeurotransmitter systems of the brain and their functions
Neurotransmitter systems of the brain and their functionsSSA KPI
 

More from SSA KPI (20)

Germany presentation
Germany presentationGermany presentation
Germany presentation
 
Grand challenges in energy
Grand challenges in energyGrand challenges in energy
Grand challenges in energy
 
Engineering role in sustainability
Engineering role in sustainabilityEngineering role in sustainability
Engineering role in sustainability
 
Consensus and interaction on a long term strategy for sustainable development
Consensus and interaction on a long term strategy for sustainable developmentConsensus and interaction on a long term strategy for sustainable development
Consensus and interaction on a long term strategy for sustainable development
 
Competences in sustainability in engineering education
Competences in sustainability in engineering educationCompetences in sustainability in engineering education
Competences in sustainability in engineering education
 
Introducatio SD for enginers
Introducatio SD for enginersIntroducatio SD for enginers
Introducatio SD for enginers
 
DAAD-10.11.2011
DAAD-10.11.2011DAAD-10.11.2011
DAAD-10.11.2011
 
Talking with money
Talking with moneyTalking with money
Talking with money
 
'Green' startup investment
'Green' startup investment'Green' startup investment
'Green' startup investment
 
From Huygens odd sympathy to the energy Huygens' extraction from the sea waves
From Huygens odd sympathy to the energy Huygens' extraction from the sea wavesFrom Huygens odd sympathy to the energy Huygens' extraction from the sea waves
From Huygens odd sympathy to the energy Huygens' extraction from the sea waves
 
Dynamics of dice games
Dynamics of dice gamesDynamics of dice games
Dynamics of dice games
 
Energy Security Costs
Energy Security CostsEnergy Security Costs
Energy Security Costs
 
Naturally Occurring Radioactivity (NOR) in natural and anthropic environments
Naturally Occurring Radioactivity (NOR) in natural and anthropic environmentsNaturally Occurring Radioactivity (NOR) in natural and anthropic environments
Naturally Occurring Radioactivity (NOR) in natural and anthropic environments
 
Advanced energy technology for sustainable development. Part 5
Advanced energy technology for sustainable development. Part 5Advanced energy technology for sustainable development. Part 5
Advanced energy technology for sustainable development. Part 5
 
Advanced energy technology for sustainable development. Part 4
Advanced energy technology for sustainable development. Part 4Advanced energy technology for sustainable development. Part 4
Advanced energy technology for sustainable development. Part 4
 
Advanced energy technology for sustainable development. Part 3
Advanced energy technology for sustainable development. Part 3Advanced energy technology for sustainable development. Part 3
Advanced energy technology for sustainable development. Part 3
 
Advanced energy technology for sustainable development. Part 2
Advanced energy technology for sustainable development. Part 2Advanced energy technology for sustainable development. Part 2
Advanced energy technology for sustainable development. Part 2
 
Advanced energy technology for sustainable development. Part 1
Advanced energy technology for sustainable development. Part 1Advanced energy technology for sustainable development. Part 1
Advanced energy technology for sustainable development. Part 1
 
Fluorescent proteins in current biology
Fluorescent proteins in current biologyFluorescent proteins in current biology
Fluorescent proteins in current biology
 
Neurotransmitter systems of the brain and their functions
Neurotransmitter systems of the brain and their functionsNeurotransmitter systems of the brain and their functions
Neurotransmitter systems of the brain and their functions
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Erlang intro

  • 1. Introduction to Functional Programming Vasilij Savin AACIMP 2011
  • 2.
  • 3. Background: Telecom issues ● No down time allowed ● Solution has to be VERY scalable (millions operations per second) ● Maintainable software ● Distributed systems
  • 4. History of Erlang ● Need for robust fault-tolerant language ● Ericsson did not find one to suit the needs ● Based originally on Prolog – Inherits many syntax concepts ● Later rewritten with C for performance ● Opensource from 2001
  • 5. Erlang distinguished properties ● Concurrency ● Hot code loading ● Distributed ● Soft real-time ● Robust
  • 6. What Erlang is NOT? ● Object-oriented ● C syntax follower ● Shared memory ideology follower ● SILVER BULLET
  • 7. What Erlang is NOT good for? ● Front-end web development ● GUI programming ● Desktop applications
  • 8. Erlang in the Real World: Companies ● Klarna (e-commerce) ● Mobile Arts (telecom) ● Ericsson (telecom) ● Goldman Sachs (banking) ● Facebook (social network)
  • 9. Erlang in the Real World: Products ● Ejabberd (instant messanging server) ● YAWS (webserver) ● Facebook chat ● Firmware in Ericsson products ● Varios telecom services
  • 10. Paradigm comparison Imperative (structured, Functional procedural) Treats computation as the Computation in terms of evaluation of mathematical statements that directly functions avoiding state change a program state and mutable data. Object-oriented Treats datafields as "objects" manipulated only through pre-defined methods
  • 11. The biggest challenge ahead: Unlearning what you already know
  • 12. Basic Concepts ● Functions are First Class citizens ● Functions are purely functional ● Iteration is achieved through recursion ● Execution Flow control is done with pattern matching
  • 13. Data Types ● Integers ● Floats ● Atoms ● Lists ● Tuples
  • 14. Integers ● B#Val is used to store > 16#FF. numbers in base < B > 255 ● $Char - ascii value > $B. ● Large integers are converted 66 to bignums ● Max size depends on physical constraints
  • 15. Floats ● Calculations are not very -123.456 efficient 12.24E-10 ● Stored as double ● Follow IEEE 754 standard
  • 16. Atoms ● Atoms are constant literals magic_tag ● Start with a lower case letter 'A quoted tag' or are encapsulated by ’ ’ '_can include blanks' ● Any character code is allowed within an atom if using ’ ’ ● Letters integers and _ are allowed if the atom starts with a lower case letter
  • 17. Tuples ● Tuples are used to store a {123, bcd} fixed number of items {123, def, abc} ● Tuples of any size are allowed {person, 'Bass', 'Savin'} ● Must contain valid Erlang {abc, {def, 12.3}, “jkl”} expressions {} ● Tuple Arity – number of elements in tuple
  • 18. Lists ● Used to store a variable [1,2,'quoted number of items Atom',atom,"str",123.12, {tuple,1}] ● Lists are dynamically sized > [97,98,99,4,5,6]. ● Strings in Erlang are lists of ASCII values [97,98,99,4,5,6] ● Lists are written beginning > [233]. with a [ and ending with a ] "é" ● Elements are separated by > [65,66,68]. commas "ABD"
  • 19. Lists II ● A recursive list definition “ABC” “DEF” == “ABCDEF” consists of a head and a tail Not recommended ● Lists whose last Tail term is [] (inefficient) are called: proper lists > [1,2,3,4] – [2,3]. ● Appended automatically [1,4] ● Improper lists are of limited > [1,2,3] ++ [4,5,6]. use [1,2,3,4,5,6]
  • 20. Variables ● Variables start with an Upper A_Variable_name Case Letter or _. AnotherWayForVariable ● They may not contain any _do_not_care_variable special characters. _ ● '_' alone is a don’t care Variable. Its values are ignored and never bound. ● The value of a variable can not be changed once it hasbeen bound. ● Erlang does not have a type system. ● Types are determined at run time.
  • 21. Modules ● Modules are stored in files with the .erl extension ● The module and file names must be the same ● Exported functions can be called from outside the module ● Use the module prefix when making the call – <module>:<function_name>([arg list]). ● Local functions can only be called within the module
  • 23. Functions ● Erlang programs consist of functions ● Functions are defined within Modules ● Function and module names must be atoms ● A function is defined as a collection of clauses ● Variables are pattern matched in the function head. If pattern matching fails on a clause, the next one is tested. One clause must always succeed ● Functions return value of the last expression executed
  • 25. Pattern Matching: <Pattern> = <Expression> Pattern Matching is used for: ● Assigning values to Variables ● Controlling the execution flow of the programs ● Extracting values from compound data types
  • 26. Pattern Matching: Assignment ● The Pattern can containunbound variables which are bound when the A = 10. pattern matching succeeds Str = “nice string”. ● The Expression may not List = [1,2,3]. contain unbound variables Tuple = {abc, List, Str} BadTuple = {List, Data} fails
  • 27. Pattern Matching: Testing ● A match must either succeed {A, A, B} = {10, 20, or fail “str”} ● Used to pick the execution fails flow in: [A, B] = [1, 2, 3, 4] – case statements fails – receive statements [A, B | C] = [1,2,3,4,5] – function heads succeeds A = 1, B = 2, C = [3,4,5]
  • 28. Pattern Matching: Value Extraction ● Used to extract values from complex data types ● Note the use of _, the don't care variable {_, Name, {Street, City, _}} = {person, “Vasilij”, {“Kantatvagen”, 'Stockholm', sweden}}
  • 29.
  • 31. Case statement ● One branch must always succeed ● By putting the ‘_’ or an unbound variable in the last clause ensures that the clause will always be picked should the previous ones not match ● The _ clause is not mandatory
  • 32. Guards ● Guards are additional clauses that can go in a function's head to make pattern matching more expressive. ● All variables in guards have to be bound ● If all guards have to succeed, use , to separate them ● If one guard has to succeed, use ; to separate them ● Guards have to be free of side effects ● There are restrictions on BIFS and expressions in guards ● User functions are not allowed in guards foo(String) when is_list(String), length(String) < 10 → ... foo(Tuple) when is_tuple(Tuple);is_atom(Tuple) → ...
  • 33. Recursion examples odd([Head|Tail]) when Head rem 1 == 0 -> [Head|even(Tail)]; odd([_Head|Tail]) -> even(Tail); odd([]) -> [].
  • 34. Recursion with accumulators average(X) -> average(X, 0, 0). average([H|T], Length, Sum) -> average(T, Length + 1, Sum + H); average([], Length, Sum) -> Sum / Length.
  • 35. Runtime Errors ● badarg – BIF with wrong arguments is called ● badmatch – patternmatching failed and clause list is exhausted ● function clause – no function clause matched the pattern
  • 36. Useful libraries ● io.erl – general I/O functionality ● file.erl – general filesystem functionality ● lists.erl – helpful list function More documentation can be found here: http://www.erlang.org/doc/