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

A brief introduction to functional programming

  • 1.
    A brief introductionto functional programming Sebastian Wild Stylight GmbH October 31, 2012
  • 2.
    Functional programming languages Someprogramming concepts Variables and Functions Currying Functions as parameters Accumulators Advanced examples Summary
  • 3.
    And that languageis 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 Tailrecursive: # 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