SlideShare uma empresa Scribd logo
1 de 16
Baixar para ler offline
A brief introduction to functional programming

                 Sebastian Wild

                  Stylight GmbH


                October 31, 2012
Functional programming languages


Some programming concepts
   Variables and Functions
   Currying
   Functions as parameters
   Accumulators


Advanced examples


Summary
And that language is fast. I mean, really astoundingly
    fast, which is why it is so evil. It’s like that psychotic
    girlfriend that was amazing in bed. You keep trying to
    break up with her, but she seduces you right back into
    that abusive relationship.
from brool.com
Article: The Genius of Python, The Agony of OCaml
Functional programming languages




      Pure
      Haskell, Charity, Clean, Curry, Hope, Miranda
      Impure
      Erlang, F#, Lisp, ML (Standard ML, OCaml), Scala,
      Spreadsheets
First program




   # p r i n t s t r i n g ” H e l l o , World ! ” ; ;
   H e l l o , World : u n i t = ( )
Variables and Functions



   # let a = 5;;
   val a : int = 5

   # let a = 5 + 3;;
   val a : int = 8

   (∗ ! ! A t t e n t i o n ! ! Strong type system ∗)
   # let a = 5 + 3.0;;
Variables and Functions




   # l e t a = (5 , () , ” Hello ” ) ; ;
   v a l a : i n t ∗ u n i t ∗ s t r i n g = (5 , ( ) , ” H e l l o ”)

   # let f x = x ∗ x ;;
   v a l f : i n t −> i n t = <fun>
Currying




  # let f (x , y) = x ∗ y ; ;
  v a l f : i n t ∗ i n t −> i n t = <fun>
Currying




  # let f x y = x ∗ y ;;
  v a l f : i n t −> i n t −> i n t = <fun>

   ( ∗ Types a r e r i g h t b r a c k e t e d ∗ )
   i n t −> ( i n t −> i n t )
Currying


  # let f x y = x ∗ y ;;

  # let a = f 5;;
  v a l a : i n t −> i n t = <fun>

  # let b = a 6;;
  v a l b : i n t = 30

  (∗ Function c a l l s are l e f t b r a c k e t e d ∗)
  # v a l b = ( f 5) 6 ; ;
  v a l b : i n t = 30
Functions as parameters


   # let a = [1;2;3;4];;
   val a : int l i s t = [ 1 ; 2; 3; 4]

   # l e t r e c map f s =
         match s w i t h
                 [ ] −> [ ]
               | x : : x s −> ( f x ) : : ( map f x s ) ; ;
   v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>

   # l e t b = map ( f u n v −> 2 ∗ v ) a ; ;
   val b : int l i s t = [ 2 ; 4; 6; 8]
Functions as parameters




   L i s t . f o l d l e f t : ( ’ a −> ’ b −> ’ a ) −> ’ a −> ’ b l i s t −

   L i s t . f o l d l e f t f a [ b1 ; . . . ; bn ]
   f ( . . . ( f ( f a b1 ) b2 ) . . . )

   # L i s t . f o l d l e f t (+) 0 [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ] ; ;
   − : i n t = 21
Accumulators




   Faculty of n:
  # l e t rec fac n =
        i f n <= 0 t h e n 1
        else n ∗ fac (n − 1 ) ; ;
  v a l f a c : i n t −> i n t = <fun>
Accumulator



  Tail recursive:
  # l e t fac n =
        l e t r e c fac ’ acc n =
              i f n <= 0 t h e n a c c
              e l s e f a c ’ ( n∗ a c c ) ( n−1) i n
        fac ’ 1 n ; ;
  v a l f a c : i n t −> i n t = <fun>
Advanced examples




  http://www.ffconsultancy.com/ocaml/bunny/index.html
  http://www.ffconsultancy.com/ocaml/ray tracer/index.html
Summary




      No side effects
      Strong type system
      Mathematical functions can be expressed very easily
      Steep learning curve
  https://github.com/wildsebastian/AlgorithmsDataStructures

Mais conteúdo relacionado

Mais procurados

Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...SAKSHISINGH486
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functionalHackraft
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macrosMohammed Sikander
 
6 more things about Perl 6
6 more things about Perl 66 more things about Perl 6
6 more things about Perl 6brian d foy
 
basic shell_programs
 basic shell_programs basic shell_programs
basic shell_programsmadhugvskr
 
Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)James Titcumb
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit partsMaxim Zaks
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyBeScala
 
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)James Titcumb
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1Hackraft
 
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)James Titcumb
 
アプリカティブファンクターとHaskell 2014版
アプリカティブファンクターとHaskell 2014版アプリカティブファンクターとHaskell 2014版
アプリカティブファンクターとHaskell 2014版infinite_loop
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operatorsHarleen Sodhi
 
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)James Titcumb
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 

Mais procurados (20)

Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...Torchbearersnotebook.blogspot.com program to create a list in python and valu...
Torchbearersnotebook.blogspot.com program to create a list in python and valu...
 
Swift Rocks #2: Going functional
Swift Rocks #2: Going functionalSwift Rocks #2: Going functional
Swift Rocks #2: Going functional
 
week-16x
week-16xweek-16x
week-16x
 
Questions typedef and macros
Questions typedef and macrosQuestions typedef and macros
Questions typedef and macros
 
6 more things about Perl 6
6 more things about Perl 66 more things about Perl 6
6 more things about Perl 6
 
Session07 recursion
Session07 recursionSession07 recursion
Session07 recursion
 
basic shell_programs
 basic shell_programs basic shell_programs
basic shell_programs
 
Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)Interpret this... (PHPem 2016)
Interpret this... (PHPem 2016)
 
Swift the implicit parts
Swift the implicit partsSwift the implicit parts
Swift the implicit parts
 
Ozma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrencyOzma: Extending Scala with Oz concurrency
Ozma: Extending Scala with Oz concurrency
 
Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)Climbing the Abstract Syntax Tree (PHP UK 2018)
Climbing the Abstract Syntax Tree (PHP UK 2018)
 
Swift rocks! #1
Swift rocks! #1Swift rocks! #1
Swift rocks! #1
 
week-19x
week-19xweek-19x
week-19x
 
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
Climbing the Abstract Syntax Tree (ScotlandPHP 2018)
 
Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]Data Structures - Lecture 6 [queues]
Data Structures - Lecture 6 [queues]
 
アプリカティブファンクターとHaskell 2014版
アプリカティブファンクターとHaskell 2014版アプリカティブファンクターとHaskell 2014版
アプリカティブファンクターとHaskell 2014版
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)Climbing the Abstract Syntax Tree (Southeast PHP 2018)
Climbing the Abstract Syntax Tree (Southeast PHP 2018)
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Final ds record
Final ds recordFinal ds record
Final ds record
 

Semelhante a A brief introduction to functional programming

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific ComputingAlbert DeFusco
 
Python Fundamentals - Basic
Python Fundamentals - BasicPython Fundamentals - Basic
Python Fundamentals - BasicWei-Yuan Chang
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fuclimatewarrior
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义leejd
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler DevelopmentLogan Chien
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughterQuinn Wilton
 
Haskell - Being lazy with class
Haskell - Being lazy with classHaskell - Being lazy with class
Haskell - Being lazy with classTiago Babo
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfsagar414433
 
Python_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfPython_Cheat_Sheet_Keywords_1664634397.pdf
Python_Cheat_Sheet_Keywords_1664634397.pdfsagar414433
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#Dmitri Nesteruk
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataAnne Nicolas
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 

Semelhante a A brief introduction to functional programming (20)

Python for Scientific Computing
Python for Scientific ComputingPython for Scientific Computing
Python for Scientific Computing
 
Python Fundamentals - Basic
Python Fundamentals - BasicPython Fundamentals - Basic
Python Fundamentals - Basic
 
Python quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung FuPython quickstart for programmers: Python Kung Fu
Python quickstart for programmers: Python Kung Fu
 
JavaFX, because you're worth it
JavaFX, because you're worth itJavaFX, because you're worth it
JavaFX, because you're worth it
 
ch08.ppt
ch08.pptch08.ppt
ch08.ppt
 
Python 培训讲义
Python 培训讲义Python 培训讲义
Python 培训讲义
 
Music as data
Music as dataMusic as data
Music as data
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
Introduction to Compiler Development
Introduction to Compiler DevelopmentIntroduction to Compiler Development
Introduction to Compiler Development
 
Software to the slaughter
Software to the slaughterSoftware to the slaughter
Software to the slaughter
 
Haskell - Being lazy with class
Haskell - Being lazy with classHaskell - Being lazy with class
Haskell - Being lazy with class
 
Java operators
Java operatorsJava operators
Java operators
 
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
 
Functional Programming in F#
Functional Programming in F#Functional Programming in F#
Functional Programming in F#
 
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary dataKernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
Kernel Recipes 2019 - GNU poke, an extensible editor for structured binary data
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 

Último

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
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...DianaGray10
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
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 2024Victor Rentea
 
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 WorkerThousandEyes
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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
 
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 FMESafe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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.pdfOrbitshub
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
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 FMESafe Software
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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 businesspanagenda
 

Último (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
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
 
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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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, ...
 
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
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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
 

A brief introduction to functional programming

  • 1. A brief introduction to functional programming Sebastian Wild Stylight GmbH October 31, 2012
  • 2. Functional programming languages Some programming concepts Variables and Functions Currying Functions as parameters Accumulators Advanced examples Summary
  • 3. And that language is fast. I mean, really astoundingly fast, which is why it is so evil. It’s like that psychotic girlfriend that was amazing in bed. You keep trying to break up with her, but she seduces you right back into that abusive relationship. from brool.com Article: The Genius of Python, The Agony of OCaml
  • 4. Functional programming languages Pure Haskell, Charity, Clean, Curry, Hope, Miranda Impure Erlang, F#, Lisp, ML (Standard ML, OCaml), Scala, Spreadsheets
  • 5. First program # p r i n t s t r i n g ” H e l l o , World ! ” ; ; H e l l o , World : u n i t = ( )
  • 6. Variables and Functions # let a = 5;; val a : int = 5 # let a = 5 + 3;; val a : int = 8 (∗ ! ! A t t e n t i o n ! ! Strong type system ∗) # let a = 5 + 3.0;;
  • 7. Variables and Functions # l e t a = (5 , () , ” Hello ” ) ; ; v a l a : i n t ∗ u n i t ∗ s t r i n g = (5 , ( ) , ” H e l l o ”) # let f x = x ∗ x ;; v a l f : i n t −> i n t = <fun>
  • 8. Currying # let f (x , y) = x ∗ y ; ; v a l f : i n t ∗ i n t −> i n t = <fun>
  • 9. Currying # let f x y = x ∗ y ;; v a l f : i n t −> i n t −> i n t = <fun> ( ∗ Types a r e r i g h t b r a c k e t e d ∗ ) i n t −> ( i n t −> i n t )
  • 10. Currying # let f x y = x ∗ y ;; # let a = f 5;; v a l a : i n t −> i n t = <fun> # let b = a 6;; v a l b : i n t = 30 (∗ Function c a l l s are l e f t b r a c k e t e d ∗) # v a l b = ( f 5) 6 ; ; v a l b : i n t = 30
  • 11. Functions as parameters # let a = [1;2;3;4];; val a : int l i s t = [ 1 ; 2; 3; 4] # l e t r e c map f s = match s w i t h [ ] −> [ ] | x : : x s −> ( f x ) : : ( map f x s ) ; ; v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun> # l e t b = map ( f u n v −> 2 ∗ v ) a ; ; val b : int l i s t = [ 2 ; 4; 6; 8]
  • 12. Functions as parameters L i s t . f o l d l e f t : ( ’ a −> ’ b −> ’ a ) −> ’ a −> ’ b l i s t − L i s t . f o l d l e f t f a [ b1 ; . . . ; bn ] f ( . . . ( f ( f a b1 ) b2 ) . . . ) # L i s t . f o l d l e f t (+) 0 [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ] ; ; − : i n t = 21
  • 13. Accumulators Faculty of n: # l e t rec fac n = i f n <= 0 t h e n 1 else n ∗ fac (n − 1 ) ; ; v a l f a c : i n t −> i n t = <fun>
  • 14. Accumulator Tail recursive: # l e t fac n = l e t r e c fac ’ acc n = i f n <= 0 t h e n a c c e l s e f a c ’ ( n∗ a c c ) ( n−1) i n fac ’ 1 n ; ; v a l f a c : i n t −> i n t = <fun>
  • 15. Advanced examples http://www.ffconsultancy.com/ocaml/bunny/index.html http://www.ffconsultancy.com/ocaml/ray tracer/index.html
  • 16. Summary No side effects Strong type system Mathematical functions can be expressed very easily Steep learning curve https://github.com/wildsebastian/AlgorithmsDataStructures