Functional Programming 之二三事

3,470 views
3,378 views

Published on

2009 Taiwan Yahoo! Intern share by Leeheng.

Published in: Technology
2 Comments
20 Likes
Statistics
Notes
  • I can't compile the Haskell code on page 91, which is:
    ---------------------------------------------
    fib = 0 : 1 : zipWith (+) fibs (tail fibs)
    ---------------------------------------------

    I run it with runhaskell and it outputs
    ---------------------------------------------
    a.hs:2:27: Not in scope: `fibs'

    a.hs:2:38: Not in scope: `fibs'
    ---------------------------------------------

    Should I set any compiler flag to enable this feature? Thanks!
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Very great
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
3,470
On SlideShare
0
From Embeds
0
Number of Embeds
811
Actions
Shares
0
Downloads
179
Comments
2
Likes
20
Embeds 0
No embeds

No notes for slide

Functional Programming 之二三事

  1. 1. Functional Programming Leeheng July 31, 2009
  2. 2. Functional Bet Programming a Leeheng July 31, 2009
  3. 3. Agenda About Myself Why This Topic Programming Paradigm 10min PL Moving On Why FP Matters IP VS DP Function, Lambda Calculus High Order Function, Currying No Side Effect, Referential Transparent Least Surprise, No Update 25min Recursion, Lazy Evaluation Pattern Matching, Tail recursion Memoization, Closure Loop Helper Example Story 25min Reference Question 2
  4. 4. Agenda About Myself Why This Topic Programming Paradigm 10min PL Moving On Why FP Matters IP VS DP Function, Lambda Calculus High Order Function, Currying No Side Effect, Referential Transparent Least Surprise, No Update 25min Recursion, Lazy Evaluation Pattern Matching, Tail recursion Memoization, Closure Loop Helper Example Story 25min Reference Question 2
  5. 5. Agenda About Myself Why This Topic Programming Paradigm 10min PL Moving On Why FP Matters IP VS DP Function, Lambda Calculus High Order Function, Currying No Side Effect, Referential Transparent Least Surprise, No Update 25min Recursion, Lazy Evaluation Pattern Matching, Tail recursion Memoization, Closure Loop Helper Example Story 25min Reference Question 2
  6. 6. Agenda About Myself Why This Topic Programming Paradigm 10min PL Moving On Why FP Matters IP VS DP Function, Lambda Calculus High Order Function, Currying No Side Effect, Referential Transparent Least Surprise, No Update 25min Recursion, Lazy Evaluation Pattern Matching, Tail recursion Memoization, Closure Loop Helper Example Story 25min Reference Question 2
  7. 7. ABOUT MYSELF Leeheng Ma Y! 2009 Search Team Intern NCCU MIS undergraduate Interests : Algorithm, Programming language, Compiler, Web Technology. 3
  8. 8. WHY THIS TOPIC 4
  9. 9. WHY THIS TOPIC .... 4
  10. 10. WHY THIS TOPIC .... Programming Language RD 4
  11. 11. Programming Paradigm Programming paradigm is a Fundamental style of computer programming. 5
  12. 12. Programming Paradigm Programming paradigm is a Fundamental style of computer programming. For solving Software Engineering Problems. 5
  13. 13. Programming Paradigm Programming paradigm is a Fundamental style of computer programming. For solving Software Engineering Problems. Programming Paradigm SHIFT 5
  14. 14. Think Different • A language that doesn't affect the way you think about programming, is not worth knowing. --Alan Perlis 6
  15. 15. Think Different • A language that doesn't affect the way you think about programming, is not worth knowing. --Alan Perlis 6
  16. 16. Think Different • A language that doesn't affect the way you think about programming, is not worth knowing. --Alan Perlis 6
  17. 17. HOW MANY YOU KNOW ? • Concurrent • Imperative • Declarative • Functional • Procedural Programming • Prototype-based • Event-driven • Aspect-Oriented • Object-Oriented • Meta-Programming • Generic 7
  18. 18. HOW MANY YOU KNOW ? ... Orz ..... ... 7
  19. 19. WHERE PL MOVING ON ? 8
  20. 20. WHERE PL MOVING ON ? 8
  21. 21. WHERE PL MOVING ON ? 8
  22. 22. WHERE PL MOVING ON ? 8
  23. 23. WHERE PL MOVING ON ? 8
  24. 24. WHERE PL MOVING ON ? 8
  25. 25. WHERE PL MOVING ON ? 8
  26. 26. WHERE PL MOVING ON ? 8
  27. 27. WHERE PL MOVING ON ? 8
  28. 28. WHERE PL MOVING ON ? 8
  29. 29. WHERE PL MOVING ON ? 8
  30. 30. WHERE PL MOVING ON ? 8
  31. 31. WHERE PL MOVING ON ? 8
  32. 32. WHERE PL MOVING ON ? 8
  33. 33. WHY FP MATTERS 9
  34. 34. WHY FP MATTERS • Python, Ruby, Perl all use ideas form FP • Y! ’s Hadoop and G ‘s MapReduce are large scale FP 9
  35. 35. WHY FP MATTERS • Python, Ruby, Perl all use ideas form FP • Y! ’s Hadoop and G ‘s MapReduce are large scale FP Modularity Interchangeable parts Easy test and debug Easy optimization Function as data • No Side Effect Pattern Matching Concurrent Processing • High Order Function Closure Lazy Evaluation Referential transparency 9
  36. 36. IP VS FP • Imperative Programming – Computation Executing statements to change state. – Program Consists of a sequence of commands. • Functional Programming – Computation Evaluation of expressions. – Expression Formed by using functions to combine basic values. The focus is on what, not how 10
  37. 37. IP VS FP OO C+ P • Imperative Programming Java + – Computation Executing statements to change state. – Program Consists of a sequence of commands. • Functional Programming – Computation Evaluation of expressions. – Expression Formed by using functions to combine basic values. The focus is on what, not how 10
  38. 38. IP VS FP OO C+ P • Imperative Programming Java + – Computation Executing statements to change state. – Program Consists of a sequence of commands. g Lisp Ha E rlan • Functional Programming ske ll – Computation Evaluation of expressions. – Expression Formed by using functions to combine basic values. The focus is on what, not how 10
  39. 39. Create Sandwich Imperative Functional 11
  40. 40. Create Sandwich Imperative 1. Take a bread 2. Spread bread with butter 3. Put cheese on the bread 4. return sandwich Functional 11
  41. 41. Create Sandwich Imperative 1. Take a bread 2. Spread bread with butter 3. Put cheese on the bread 4. return sandwich Functional return put ( cheese, spread(butter, bread) ) 11
  42. 42. Features 12
  43. 43. Function is ? In computer science, a subroutine or subprogram (also called procedure, method, function, or routine) is a portion of code within a larger program, which performs a specific task and is relatively independent of the remaining code. 12
  44. 44. Function is ? The mathematical concept of a function expresses the intuitive idea that one quantity (the argument of the function, also known as the input) completely determines another quantity (the value, or output). 12
  45. 45. Function is ? The mathematical concept of a function expresses the intuitive idea that one quantity (the argument of the function, also known as the input) completely determines another quantity (the value, or output). 12
  46. 46. Function is ? The mathematical concept of a function expresses the intuitive idea that one quantity (the argument of the function, also known as the input) completely determines another quantity (the value, or output). So we minimize function!! 12
  47. 47. Lambda Calculus The λ-calculus was developed by the logician Alonzo Church in 1930’s as a tool to study functions and computability. 13
  48. 48. λ Calculus Equivalent to Turing Machines Turing 1937 Equivalent to recursive functions Kleene 1936 (λ x. x + 2) 14
  49. 49. λ Calculus Equivalent to Turing Machines Turing 1937 Equivalent to recursive functions Kleene 1936 (λ x. x + 2) = (x) { return x + 2} 14
  50. 50. λ Calculus Equivalent to Turing Machines Turing 1937 Equivalent to recursive functions Kleene 1936 (λ x. x + 2) = (x) { return x + 2} (λ x. x + 2) 3 = 5 14
  51. 51. λ Calculus Equivalent to Turing Machines Turing 1937 Equivalent to recursive functions Kleene 1936 (λ f. f 3)(λ x. x+2) = ? 14
  52. 52. λ Calculus Equivalent to Turing Machines Turing 1937 Equivalent to recursive functions Kleene 1936 (λ f. f 3)(λ x. x+2) = ? = (λ x. x + 2) 3 =3+2 14
  53. 53. High Order Function • Functions take other functions as parameters • Functions return functions as results 15
  54. 54. High Order Function • Functions take other functions as parameters ∑ f(x) x∈L • Functions return functions as results 15
  55. 55. High Order Function • Functions take other functions as parameters ∑ f(x) x∈L • Functions return functions as results d f(x) dx = f’(x) 15
  56. 56. Mathematics 16
  57. 57. Mathematics OK, no more mathematics. 16
  58. 58. Design Pattern 17
  59. 59. Design Pattern Adapter Pattern 17
  60. 60. Design Pattern Adapter Pattern 17
  61. 61. Design Pattern Adapter Pattern + 17
  62. 62. Design Pattern Adapter Pattern 17
  63. 63. Design Pattern Adapter Pattern 17
  64. 64. Currying ( ) AJAX callback function 18
  65. 65. No Side Effect Output is solely dependent of input. input output Pure Function 19
  66. 66. No Side Effect Output is solely dependent of input. States input output Impure Function With Assignment 19
  67. 67. No Side Effect Output is solely dependent of input. States input output Impure Function With Assignment Can replace f(x) + f(x) with 2*f(x)? 19
  68. 68. Referential Transparent If the function f is referential transparent. 20
  69. 69. Referential Transparent If the function f is referential transparent. What is this? 20
  70. 70. Referential Transparent What is this? Definition: If Function result depends only on the values of its parameters. 20
  71. 71. Referential Transparent What is this? Definition: If Function result depends only on the values of its parameters. Murmur: This concept occurs in mathematics, but it is broken by imperative programming languages. 20
  72. 72. Side Effect Consider the following C/Java function f: 21
  73. 73. Side Effect Consider the following C/Java function f: f change the state outside the function. 21
  74. 74. Principle Of Least Surprise All possible inputs are listed in the parameter list, noworries about global state. Single assignment means that you never have to find where a variable updated, cause it is only defined one place in the entire function. Small function, easy to testing. (suit with TDD) 22
  75. 75. No Update On Variables Definition: Variables are similar to Variables in mathematics, they hold a value but they can’t be updated. Variables only assigned once. Pure 23
  76. 76. No Update On Variables Definition: Variables are similar to Variables in mathematics, they hold a value but they can’t be updated. Variables only assigned once. Pure 23
  77. 77. No Update On Variables Definition: Variables are similar to Variables in mathematics, they hold a value but they can’t be updated. Variables only assigned once. So...We don’t need to care call by value, call by reference...etc. Pure 23
  78. 78. No Update On Variables Definition: Variables are similar to Variables in mathematics, they hold a value but they can’t be updated. Variables only assigned once. So...We don’t need to care call by value, call by reference...etc. Pure 23
  79. 79. Recursion Basics How to Loop? Functional programs are usually H . (with map, fold, filter) Pure 24
  80. 80. Recursion Basics How to Loop? Functional programs are usually Heavily Recursive. (with map, fold, filter) Pure 24
  81. 81. Recursion Basics How to Loop? Functional programs are usually Heavily Recursive. (with map, fold, filter) Iteration.isEqualTo( Recursive); Pure 24
  82. 82. Efficiency Early: It is true... Now: Tail Recursion. Lazy Evaluation. Memoization Common Subexpression Elimination Parallel Computing 25
  83. 83. Efficiency Early: It is true... Now: Tail Recursion. Lazy Evaluation. Memoization Common Subexpression Elimination Parallel Computing 25
  84. 84. Efficiency Early: It is true... Now: Tail Recursion. != Lazy Evaluation. Memoization Common Subexpression Elimination Parallel Computing 25
  85. 85. Efficiency Early: It is true... Now: Tail Recursion. Lazy Evaluation. Memoization Common Subexpression Elimination Parallel Computing 25
  86. 86. Efficiency Early: It is true... Now: Tail Recursion. Lazy Evaluation. Memoization Common Subexpression Elimination Parallel Computing 25
  87. 87. Chat in Pugs Project From freenode, #perl6, 2005/3/2 http://xrl.us/e98m 19:08 < malaire> Does pugs yet have system() or backticks or qx// or any way to use system commands? 19:08 < autrijus> malaire: no, but I can do one for you now. a sec 19:09 < malaire> ok, I'm still reading YAHT, so I won't try to patch pugs just yet... 19:09 < autrijus> you want unary system or list system? 19:09 < autrijus> system("ls -l") vs system("ls", "-l") 19:10 < malaire> perhaps list, but either is ok 19:11 < autrijus> n Bool pre system (Str) 19:11 < autrijus> n Bool pre system (Str: List) 19:11 < autrijus> I'll do both :) 19:11 < autrijus> done. testing. 19:14 < autrijus> test passed. r386. enjoy 19:14 < malaire> that's quite fast development :) 19:14 < autrijus> :) 26
  88. 88. Lazy Evaluation lazy evaluation is the technique of delaying a computation until the result is required. Benefits: S1 = EXPR 1(); S2 = EXPR 2(); S2 = Concatenate(S1, S2); Pure 27
  89. 89. Lazy Evaluation lazy evaluation is the technique of delaying a computation until the result is required. Benefits: S1 = EXPR 1(); S2 = EXPR 2(); S2 = Concatenate(S1, S2); Pure 27
  90. 90. Lazy Evaluation lazy evaluation is the technique of delaying a computation until the result is required. Benefits: In Haskell, we don’t run S1 = EXPR 1(); S1 and S2 before we call Concatenate, we S2 = EXPR 2(); even don’t know which S2 = Concatenate(S1, S2); one evaluate first! Pure 27
  91. 91. Lazy Evaluation lazy evaluation is the technique of delaying a computation until the result is required. Benefits: In Haskell, we don’t run S1 = EXPR 1(); S1 and S2 before we call Concatenate, we S2 = EXPR 2(); even don’t know which S2 = Concatenate(S1, S2); one evaluate first! Benefits: Pure 27
  92. 92. Pattern Matching A Good Picture Can Tell Every Thing! 28
  93. 93. Tail Recursion 29
  94. 94. Tail Recursion Stack fac 5 5 * (fac 4) 5 * 4 * (fac 3) 5 * 4 * 3 * (fac 2) 5 * 4 * 3 * 2 * (fac 1) 5 * 4 * 3 * 2 * 1 * (fac 0) 5 * 4 * 3 * 2 * 1 * 1 . . 120 Time 29
  95. 95. Tail Recursion 29
  96. 96. Tail Recursion Stack fac 5 tailfac 5 1 tailfac 4 5 * 1 tailfac 3 4 * 5 * 1 tailfac 2 3 * 4 * 5 * 1 tailfac 1 2 * 3 * 4 * 5 * 1 tailfac 0 1 * 2 * 3 * 4 * 5 * 1 . . 120 Time 29
  97. 97. Tail Recursion Stack fac 5 tailfac 5 1 tailfac 4 5 * 1 tailfac 3 4 * 20 5 * 1 tailfac 2 3 * 60 4 * 5 * 1 tailfac 1 2 * 120 3 * 4 * 5 * 1 tailfac 0 1 * 120 2 * 3 * 4 * 5 * 1 . . 120 Time 29
  98. 98. Tail Recursion Stack fac 5 tailfac 5 1 tailfac 4 5 * 1 tailfac 3 4 * 20 5 * 1 tailfac 2 3 * 60 4 * 5 * 1 tailfac 1 2 * 120 3 * 4 * 5 * 1 tailfac 0 1 * 120 2 * 3 * 4 * 5 * 1 . . Tail Recursion can be Time 120 optimize to loop!! 29
  99. 99. Memoization Definition: Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. 30
  100. 100. Memoization Definition: Memoization is an optimization technique used primarily to speed up computer programs by having function calls avoid repeating the calculation of results for previously-processed inputs. 30
  101. 101. Closure In C 31
  102. 102. Closure In C New or Malloc. 31
  103. 103. Closure In C But Global is 31
  104. 104. Closure In C Another issue: scope static and dynamic. 31
  105. 105. Closure In C Another issue: scope static and dynamic. Global I Am Symbol Table 31
  106. 106. Closure In C Another issue: scope static and dynamic. Global Local I Am I Am Symbol Table Symbol Table 31
  107. 107. Closure In C Another issue: scope static and dynamic. Global I Am Symbol Table 31
  108. 108. Closure In C Another issue: scope static and dynamic. 31
  109. 109. Closure In C Another issue: scope static and dynamic. 31
  110. 110. Closure In C Another issue: scope static and dynamic. 31
  111. 111. Loop Helper Not only Recursion, but also Loop Helper? Recursion : divided and conquer Loop : don’t do that so complicate 32
  112. 112. Folding Alias : reduce 33
  113. 113. Folding Alias : reduce Operator Initial value List 33
  114. 114. Folding Alias : reduce 33
  115. 115. Folding Alias : reduce 33
  116. 116. Folding Alias : reduce 33
  117. 117. Mapping 34
  118. 118. Mapping 34
  119. 119. Mapping 34
  120. 120. Mapping a, b, c, ..., z f(a), f(b), f(c), ..., f(z) Function f() 34
  121. 121. Mapping 34
  122. 122. Filtering Definition: if (p?) w, then send w to output a, b, c, ..., z a’, b’, c’, ..., z’ Function f() 35
  123. 123. Filtering Definition: if (p?) w, then send w to output a, b, c, ..., z a’, b’, c’, ..., z’ Function f() 35
  124. 124. Quick Sort 36
  125. 125. Quick Sort 36
  126. 126. Quick Sort 36
  127. 127. Quick Sort 36
  128. 128. Insert Sort 37
  129. 129. Insert Sort 37
  130. 130. Insert Sort 37
  131. 131. Insert Sort 37
  132. 132. Insert Sort 37
  133. 133. Story Paul Graham Develop Y! Store by Lisp. Develop Perl6 by Haskell. “ Lisp ” by Beyond Java MIT CS 6.001 Structure and Interpretation of Computer Programs 38
  134. 134. Reference • Blog – C Functional Language Currying – JavaScript Memoization – Functional Programming For The Rest of Us – Functional Programming in JavaScript and Ruby • Book – Practical Common Lisp – Haskell: The Craft of Functional Programming – The Haskell School of Expression: Learning Functional Programming through Multimedia • Web – Wikipedia – Understanding Haskell Monads 39
  135. 135. Reference • Web – DJWS – Why Functional Programming Matters • Slide – Introduction To Functional Programming – Functional Programming With Python (EuroPython 2008) – Functional Programming In Java – Understanding Functional Programming – Functional Programming – Functional Concepts for OOP Developers – Special thanks the slides form NCCU CS Chen Kung professor 40
  136. 136. Question ? 41

×