狠狠撸

狠狠撸Share a Scribd company logo
Welcome to the world of the functional programming



 Kenta Murata                   2011.06.11 #osc11do



Monday, June 13, 2011                                 1
Monday, June 13, 2011   2
mrkn



        Ruby
        Ruby



http://www.flickr.com/photos/koichiroo/5244581973/
Monday, June 13, 2011                                3
Monday, 1
2010 3 June 13, 2011   4
Welcome to the world of the functional programming



 Kenta Murata                   2011.06.11 #osc11do



Monday, June 13, 2011                                 5
Functional Programming



Monday, June 13, 2011      6
Imperative Programming

 Procedural Programming


Monday, June 13, 2011     7
Fortran
                         Basic
                         Cobol
                         Algol
                            C
                          C++
                          Java
                          LISP

Monday, June 13, 2011             8
Monday, June 13, 2011   9
Functional Programming



Monday, June 13, 2011      10
High-order Functions

                        Lazy Evaluation


Monday, June 13, 2011                     11
Functions
           as Building Blocks
   for constructing programs
http://www.flickr.com/photos/stevendepolo/5597111652
Monday, June 13, 2011                                  12
Functions as Glue
                        for bonding functions
http://www.flickr.com/photos/pseudoego/5417919481
Monday, June 13, 2011                               13
A lot of programming
                                    languages



 available for
 functional programming
http://www.flickr.com/photos/32712959@N07/3222623206
Monday, June 13, 2011                                  14
Monday, June 13, 2011   15
Monday, June 13, 2011   16
Monday, June 13, 2011   17
High-order Functions



Monday, June 13, 2011                18
Monday, June 13, 2011   19
Monday, June 13, 2011   20
Haskell                [   ]



               e.g. []
                    [1, 2, 3]

                                                    (:)




               e.g. [1]         => 1 : []
                    [1, 2, 3]   => 1 : 2 : 3 : []




Monday, June 13, 2011                                     21
(:)      LISP
               cons

               cons        Haskell



               cons :: a -> [a] -> [a]   --
               cons x [] = [x]           --
               cons x1 (x2:xs) = x1 : (cons x2 xs)




Monday, June 13, 2011                                22
cons :: a -> [a] -> [a]   --



               cons x [] = [x]           --
               cons x1 (x2:xs) = x1 : (cons x2 xs)


               //         C++
               template <typename a>
               std::list<a>
               cons(a const& x, std::list<a> const& xs);




Monday, June 13, 2011                                      23
cons :: a -> [a] -> [a]   --



               cons x [] = [x]           --
               cons x1 (x2:xs) = x1 : (cons x2 xs)




               e.g. cons 0 [1,2,3,4]
                    => x1    0, x2   1, xs    [2,3,4]




Monday, June 13, 2011                                   24
(Int)
                                sum

               sum :: [Int] -> Int
               sum [] = 0
               sum (n:ns) = n + (sum ns)




Monday, June 13, 2011                      25
cons     sum

               cons :: a -> [a] -> [a]
               cons x [] = [x]
               cons x1 (x2:xs) = x1 : (cons x2 xs)

               sum :: [Int] -> Int
               sum [] = 0
               sum (n:ns) = n + (sum ns)




Monday, June 13, 2011                                26
cons x [] = [x]
               sum    [] = 0


               cons x1 (x2:xs) = x1 : (cons x2 xs)
               sum     (n :ns) = n + (sum      ns)




Monday, June 13, 2011                                27
(Int)
                                product

               product :: [Int] -> Int
               product [] = 1
               product (n:ns) = n * product ns




Monday, June 13, 2011                            28
cons x [] = [x]
               sum     [] = 0
               product [] = 1


               cons x1 (x2:xs) = x1 : (cons x2 xs)
               sum     (n :ns) = n + (sum      ns)
               product (n :ns) = n * (product ns)




Monday, June 13, 2011                                29
(Bool)
                                     allTrue



               allTrue :: [Bool] -> Bool
               allTrue [] = True
               allTrue (b:bs) = b && allTrue ns




Monday, June 13, 2011                             30
(Bool)
                                     anyTrue



               anyTrue :: [Bool] -> Bool
               anyTrue [] = False
               anyTrue (b:bs) = b || anyTrue ns




Monday, June 13, 2011                             31
cons x    []   =   [x]
               sum       []   =   0
               product   []   =   1
               allTrue   []   =   True
               anyTrue   []   =   False


               cons x1   (x2:xs)    =   x1   :    (cons x2   xs)
               sum       (n :ns)    =   n    +    (sum       ns)
               product   (n :ns)    =   n    *    (product   ns)
               allTrue   (b :bs)    =   b    &&   (allTrue   bs)
               anyTrue   (b :bs)    =   b    ||   (anyTrue   bs)




Monday, June 13, 2011                                              32
cons x    []   =   [x]
               sum       []   =   0
               product   []   =   1
               allTrue   []   =   True
               anyTrue   []   =   False


               cons x1   (x2:xs)    =   x1   :    (cons x2   xs)
               sum       (n :ns)    =   n    +    (sum       ns)
               product   (n :ns)    =   n    *    (product   ns)
               allTrue   (b :bs)    =   b    &&   (allTrue   bs)
               anyTrue   (b :bs)    =   b    ||   (anyTrue   bs)




Monday, June 13, 2011                                              32
reduce :: (a -> [a] -> a) -> a -> [a] -> a
               reduce f x0 [] = x0
               reduce f x0 (x1:xs) = f x1 (reduce f x0 xs)




               sum       =   reduce   (+)    0
               product   =   reduce   (*)    1
               allTrue   =   reduce   (&&)   True
               anyTrue   =   reduce   (||)   False




Monday, June 13, 2011                                        33
reduce :: (a -> [a] -> a) -> a -> [a] -> a
               reduce f x0 [] = x0
               reduce f x0 (x1:xs) = f x1 (reduce f x0 xs)




               sum       =   reduce   (+)    0
               product   =   reduce   (*)    1
               allTrue   =   reduce   (&&)   True
               anyTrue   =   reduce   (||)   False




Monday, June 13, 2011                                        33
reduce :: (a -> [a] -> a) -> a -> [a] -> a
               reduce f x0 [] = x0
               reduce f x0 (x1:xs) = f x1 (reduce f x0 xs)




               sum       =   reduce   (+)    0
               product   =   reduce   (*)    1
               allTrue   =   reduce   (&&)   True
               anyTrue   =   reduce   (||)   False




Monday, June 13, 2011                                        33
reduce :: (a -> [a] -> a) -> a -> [a] -> a
               reduce f x0 [] = x0
               reduce f x0 (x1:xs) = f x1 (reduce f x0 xs)




               sum       =   reduce   (+)    0
               product   =   reduce   (*)    1
               allTrue   =   reduce   (&&)   True
               anyTrue   =   reduce   (||)   False




Monday, June 13, 2011                                        33
cons     reduce

               append :: [a] -> [a] -> [a]
               append xs ys = reduce (:) ys xs




               append [1,2] [3,4]
               -> reduce (:) [3,4] [1,2]
               -> reduce (:) [3,4] (1:2:[])
               -> 1 : (reduce (:) [3,4] (2:[]))
               -> 1 : 2 : (reduce (:) [3,4] [])
               -> 1 : 2 : [3,4]
               -> [1,2,3,4]




Monday, June 13, 2011                             34
Monday, June 13, 2011   35
Monday, June 13, 2011   36
Monday, June 13, 2011   37
Functions
           as Building Blocks
   for constructing programs
http://www.flickr.com/photos/stevendepolo/5597111652
Monday, June 13, 2011                                  38
Functions as Glue
                        for bonding functions
http://www.flickr.com/photos/pseudoego/5417919481
Monday, June 13, 2011                               39
Monday, June 13, 2011   40
-- Haskell
               reduce (+) 0 [1..10]

               // C++
               int ns[] = {1,2,3,4,5,6,7,8,9,10};
               std::accumulate(ns, ns + 10, 0, &std::plus<int>);

               # Ruby
               [*1..10].inject(0, :+)




Monday, June 13, 2011                                              41
Monday, June 13, 2011   42
Lazy Evaluation



Monday, June 13, 2011                     43
Monday, June 13, 2011   44
Monday, June 13, 2011   45
http://www.sampou.org/haskell/article/whyfp.html
Monday, June 13, 2011                              46
http://www.infoq.com/interviews/john-hughes-fp
Monday, June 13, 2011                            47
http://weblog.raganwald.com/2007/03/why-why-functional-programming-matters.html
Monday, June 13, 2011                                                             48
language   evaluation   pureness

                         Haskell      lazy        pure

               Concurrent Clean       lazy        pure

                         OCaml       strict      impure

                           F#        strict      impure

                        Scheme       strict      impure



Monday, June 13, 2011                                      49
language   evaluation   pureness

                         Haskell      lazy        pure

               Concurrent Clean       lazy        pure

                         OCaml       strict      impure

                           F#        strict      impure

                        Scheme       strict      impure



Monday, June 13, 2011                                      50
Monday, June 13, 2011   51
Monday, 1
2010 3 June 13, 2011   52
Functional Programming



Monday, June 13, 2011      53
A lot of programming
                                    languages



 available for
 functional programming
http://www.flickr.com/photos/32712959@N07/3222623206
Monday, June 13, 2011                                  54
Monday, June 13, 2011   55
High-order Functions



Monday, June 13, 2011                56
Functions
           as Building Blocks
   for constructing programs
http://www.flickr.com/photos/stevendepolo/5597111652
Monday, June 13, 2011                                  57
Functions as Glue
                        for bonding functions
http://www.flickr.com/photos/pseudoego/5417919481
Monday, June 13, 2011                               58
Lazy Evaluation



Monday, June 13, 2011                     59
Monday, June 13, 2011   60

More Related Content

関数型プログラミングの世界

  • 1. Welcome to the world of the functional programming Kenta Murata 2011.06.11 #osc11do Monday, June 13, 2011 1
  • 3. mrkn Ruby Ruby http://www.flickr.com/photos/koichiroo/5244581973/ Monday, June 13, 2011 3
  • 4. Monday, 1 2010 3 June 13, 2011 4
  • 5. Welcome to the world of the functional programming Kenta Murata 2011.06.11 #osc11do Monday, June 13, 2011 5
  • 7. Imperative Programming Procedural Programming Monday, June 13, 2011 7
  • 8. Fortran Basic Cobol Algol C C++ Java LISP Monday, June 13, 2011 8
  • 11. High-order Functions Lazy Evaluation Monday, June 13, 2011 11
  • 12. Functions as Building Blocks for constructing programs http://www.flickr.com/photos/stevendepolo/5597111652 Monday, June 13, 2011 12
  • 13. Functions as Glue for bonding functions http://www.flickr.com/photos/pseudoego/5417919481 Monday, June 13, 2011 13
  • 14. A lot of programming languages available for functional programming http://www.flickr.com/photos/32712959@N07/3222623206 Monday, June 13, 2011 14
  • 15. Monday, June 13, 2011 15
  • 16. Monday, June 13, 2011 16
  • 17. Monday, June 13, 2011 17
  • 19. Monday, June 13, 2011 19
  • 20. Monday, June 13, 2011 20
  • 21. Haskell [ ] e.g. [] [1, 2, 3] (:) e.g. [1] => 1 : [] [1, 2, 3] => 1 : 2 : 3 : [] Monday, June 13, 2011 21
  • 22. (:) LISP cons cons Haskell cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs) Monday, June 13, 2011 22
  • 23. cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs) // C++ template <typename a> std::list<a> cons(a const& x, std::list<a> const& xs); Monday, June 13, 2011 23
  • 24. cons :: a -> [a] -> [a] -- cons x [] = [x] -- cons x1 (x2:xs) = x1 : (cons x2 xs) e.g. cons 0 [1,2,3,4] => x1 0, x2 1, xs [2,3,4] Monday, June 13, 2011 24
  • 25. (Int) sum sum :: [Int] -> Int sum [] = 0 sum (n:ns) = n + (sum ns) Monday, June 13, 2011 25
  • 26. cons sum cons :: a -> [a] -> [a] cons x [] = [x] cons x1 (x2:xs) = x1 : (cons x2 xs) sum :: [Int] -> Int sum [] = 0 sum (n:ns) = n + (sum ns) Monday, June 13, 2011 26
  • 27. cons x [] = [x] sum [] = 0 cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) Monday, June 13, 2011 27
  • 28. (Int) product product :: [Int] -> Int product [] = 1 product (n:ns) = n * product ns Monday, June 13, 2011 28
  • 29. cons x [] = [x] sum [] = 0 product [] = 1 cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns) Monday, June 13, 2011 29
  • 30. (Bool) allTrue allTrue :: [Bool] -> Bool allTrue [] = True allTrue (b:bs) = b && allTrue ns Monday, June 13, 2011 30
  • 31. (Bool) anyTrue anyTrue :: [Bool] -> Bool anyTrue [] = False anyTrue (b:bs) = b || anyTrue ns Monday, June 13, 2011 31
  • 32. cons x [] = [x] sum [] = 0 product [] = 1 allTrue [] = True anyTrue [] = False cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns) allTrue (b :bs) = b && (allTrue bs) anyTrue (b :bs) = b || (anyTrue bs) Monday, June 13, 2011 32
  • 33. cons x [] = [x] sum [] = 0 product [] = 1 allTrue [] = True anyTrue [] = False cons x1 (x2:xs) = x1 : (cons x2 xs) sum (n :ns) = n + (sum ns) product (n :ns) = n * (product ns) allTrue (b :bs) = b && (allTrue bs) anyTrue (b :bs) = b || (anyTrue bs) Monday, June 13, 2011 32
  • 34. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) False Monday, June 13, 2011 33
  • 35. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) False Monday, June 13, 2011 33
  • 36. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) False Monday, June 13, 2011 33
  • 37. reduce :: (a -> [a] -> a) -> a -> [a] -> a reduce f x0 [] = x0 reduce f x0 (x1:xs) = f x1 (reduce f x0 xs) sum = reduce (+) 0 product = reduce (*) 1 allTrue = reduce (&&) True anyTrue = reduce (||) False Monday, June 13, 2011 33
  • 38. cons reduce append :: [a] -> [a] -> [a] append xs ys = reduce (:) ys xs append [1,2] [3,4] -> reduce (:) [3,4] [1,2] -> reduce (:) [3,4] (1:2:[]) -> 1 : (reduce (:) [3,4] (2:[])) -> 1 : 2 : (reduce (:) [3,4] []) -> 1 : 2 : [3,4] -> [1,2,3,4] Monday, June 13, 2011 34
  • 39. Monday, June 13, 2011 35
  • 40. Monday, June 13, 2011 36
  • 41. Monday, June 13, 2011 37
  • 42. Functions as Building Blocks for constructing programs http://www.flickr.com/photos/stevendepolo/5597111652 Monday, June 13, 2011 38
  • 43. Functions as Glue for bonding functions http://www.flickr.com/photos/pseudoego/5417919481 Monday, June 13, 2011 39
  • 44. Monday, June 13, 2011 40
  • 45. -- Haskell reduce (+) 0 [1..10] // C++ int ns[] = {1,2,3,4,5,6,7,8,9,10}; std::accumulate(ns, ns + 10, 0, &std::plus<int>); # Ruby [*1..10].inject(0, :+) Monday, June 13, 2011 41
  • 46. Monday, June 13, 2011 42
  • 48. Monday, June 13, 2011 44
  • 49. Monday, June 13, 2011 45
  • 53. language evaluation pureness Haskell lazy pure Concurrent Clean lazy pure OCaml strict impure F# strict impure Scheme strict impure Monday, June 13, 2011 49
  • 54. language evaluation pureness Haskell lazy pure Concurrent Clean lazy pure OCaml strict impure F# strict impure Scheme strict impure Monday, June 13, 2011 50
  • 55. Monday, June 13, 2011 51
  • 56. Monday, 1 2010 3 June 13, 2011 52
  • 58. A lot of programming languages available for functional programming http://www.flickr.com/photos/32712959@N07/3222623206 Monday, June 13, 2011 54
  • 59. Monday, June 13, 2011 55
  • 61. Functions as Building Blocks for constructing programs http://www.flickr.com/photos/stevendepolo/5597111652 Monday, June 13, 2011 57
  • 62. Functions as Glue for bonding functions http://www.flickr.com/photos/pseudoego/5417919481 Monday, June 13, 2011 58
  • 64. Monday, June 13, 2011 60

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n
  45. \n
  46. \n
  47. \n
  48. \n
  49. \n
  50. \n
  51. \n
  52. \n
  53. \n
  54. \n
  55. \n
  56. \n
  57. \n
  58. \n
  59. \n
  60. \n
  61. \n
  62. \n
  63. \n
  64. \n
  65. \n
  66. \n