functional programming 之二三事
DESCRIPTION
2009 Taiwan Yahoo! Intern share by Leeheng.TRANSCRIPT
![Page 1: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/1.jpg)
Functional
LeehengJuly 31, 2009
的二三事Programming
2009年7月31日星期五
![Page 2: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/2.jpg)
Functional
LeehengJuly 31, 2009
的二三事ProgrammingBeta
2009年7月31日星期五
![Page 3: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/3.jpg)
Agenda
2
About MyselfWhy This Topic
Programming ParadigmPL Moving On
Why FP MattersIP VS DP
Function, Lambda CalculusHigh Order Function, Currying
No Side Effect, Referential TransparentLeast Surprise, No UpdateRecursion, Lazy Evaluation
Pattern Matching, Tail recursionMemoization, Closure
Loop Helper
ExampleStory
ReferenceQuestion
開場 10min
過場 25min
下場 25min
2009年7月31日星期五
![Page 4: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/4.jpg)
Agenda
2
About MyselfWhy This Topic
Programming ParadigmPL Moving On
Why FP MattersIP VS DP
Function, Lambda CalculusHigh Order Function, Currying
No Side Effect, Referential TransparentLeast Surprise, No UpdateRecursion, Lazy Evaluation
Pattern Matching, Tail recursionMemoization, Closure
Loop Helper
ExampleStory
ReferenceQuestion
開場 10min
過場 25min
下場 25min
2009年7月31日星期五
![Page 5: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/5.jpg)
Agenda
2
About MyselfWhy This Topic
Programming ParadigmPL Moving On
Why FP MattersIP VS DP
Function, Lambda CalculusHigh Order Function, Currying
No Side Effect, Referential TransparentLeast Surprise, No UpdateRecursion, Lazy Evaluation
Pattern Matching, Tail recursionMemoization, Closure
Loop Helper
ExampleStory
ReferenceQuestion
開場 10min
過場 25min
下場 25min
2009年7月31日星期五
![Page 6: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/6.jpg)
Agenda
2
About MyselfWhy This Topic
Programming ParadigmPL Moving On
Why FP MattersIP VS DP
Function, Lambda CalculusHigh Order Function, Currying
No Side Effect, Referential TransparentLeast Surprise, No UpdateRecursion, Lazy Evaluation
Pattern Matching, Tail recursionMemoization, Closure
Loop Helper
ExampleStory
ReferenceQuestion
開場 10min
過場 25min
下場 25min
2009年7月31日星期五
![Page 7: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/7.jpg)
ABOUT MYSELF
3
Y! 2009 Search Team InternLeeheng Ma
NCCU MIS undergraduateInterests : Algorithm, Programming language, Compiler,
Web Technology.
2009年7月31日星期五
![Page 8: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/8.jpg)
WHY THIS TOPIC
4
2009年7月31日星期五
![Page 9: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/9.jpg)
WHY THIS TOPIC
4
因為我沒有研究可報....
2009年7月31日星期五
![Page 10: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/10.jpg)
WHY THIS TOPIC
4
因為我沒有研究可報....
因為 Programming Language 是最貼近我們 RD 的東西
2009年7月31日星期五
![Page 11: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/11.jpg)
Programming Paradigm
5
Programming paradigm is a Fundamental style of computer programming.
2009年7月31日星期五
![Page 12: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/12.jpg)
Programming Paradigm
5
Programming paradigm is a Fundamental style of computer programming.
For solving Software Engineering Problems.
2009年7月31日星期五
![Page 13: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/13.jpg)
Programming Paradigm
5
Programming paradigm is a Fundamental style of computer programming.
For solving Software Engineering Problems.
編程典範 正在Programming Paradigm SHIFT
2009年7月31日星期五
![Page 14: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/14.jpg)
Think Different
6
• A language that doesn't affect the way you think about programming, is not worth knowing.
--Alan Perlis
2009年7月31日星期五
![Page 15: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/15.jpg)
Think Different
6
• A language that doesn't affect the way you think about programming, is not worth knowing.
--Alan Perlis
2009年7月31日星期五
![Page 16: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/16.jpg)
Think Different
6
• A language that doesn't affect the way you think about programming, is not worth knowing.
--Alan Perlis
這是圖靈獎獎杯
2009年7月31日星期五
![Page 17: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/17.jpg)
HOW MANY YOU KNOW ?
• Concurrent• Imperative• Declarative• Functional• Procedural• Prototype-based• Event-driven• Aspect-Oriented• Object-Oriented• Meta-Programming• Generic
7
Programming
2009年7月31日星期五
![Page 18: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/18.jpg)
HOW MANY YOU KNOW ?
7
其實還有很多... Orz但.....就不管了...
2009年7月31日星期五
![Page 19: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/19.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 20: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/20.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 21: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/21.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 22: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/22.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 23: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/23.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 24: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/24.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 25: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/25.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 26: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/26.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 27: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/27.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 28: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/28.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 29: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/29.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 30: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/30.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 31: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/31.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 32: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/32.jpg)
WHERE PL MOVING ON ?
8
2009年7月31日星期五
![Page 33: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/33.jpg)
WHY FP MATTERS
9
名家背書
抽象思考
2009年7月31日星期五
![Page 34: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/34.jpg)
WHY FP MATTERS
• Python, Ruby, Perl all use ideas form FP• Y!牌’s Hadoop and G牌‘s MapReduce are large
scale FP
9
名家背書
抽象思考
2009年7月31日星期五
![Page 35: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/35.jpg)
WHY FP MATTERS
• Python, Ruby, Perl all use ideas form FP• Y!牌’s Hadoop and G牌‘s MapReduce are large
scale FP
9
名家背書
抽象思考
ModularityInterchangeable partsEasy test and debugEasy optimizationFunction as dataPattern MatchingConcurrent ProcessingClosureLazy EvaluationReferential transparency
• No Side Effect• High Order Function
2009年7月31日星期五
![Page 36: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/36.jpg)
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.
10
The focus is on what, not how
2009年7月31日星期五
![Page 37: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/37.jpg)
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.
10
The focus is on what, not how
C++ JavaOOP
2009年7月31日星期五
![Page 38: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/38.jpg)
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.
10
The focus is on what, not how
C++ JavaOOP
Lisp HaskellErlang
2009年7月31日星期五
![Page 39: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/39.jpg)
Create Sandwich
11
Imperative
Functional
2009年7月31日星期五
![Page 40: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/40.jpg)
Create Sandwich
11
1. Take a bread2. Spread bread with butter3. Put cheese on the bread4. return sandwich
Imperative
Functional
2009年7月31日星期五
![Page 41: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/41.jpg)
Create Sandwich
11
1. Take a bread2. Spread bread with butter3. Put cheese on the bread4. return sandwich
Imperative
return put ( cheese, spread(butter, bread) )
Functional
2009年7月31日星期五
![Page 42: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/42.jpg)
12
Features
2009年7月31日星期五
![Page 43: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/43.jpg)
Function is ?
12
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.
2009年7月31日星期五
![Page 44: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/44.jpg)
Function is ?
12
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).
2009年7月31日星期五
![Page 45: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/45.jpg)
Function is ?
12
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).
2009年7月31日星期五
![Page 46: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/46.jpg)
Function is ?
12
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!!
2009年7月31日星期五
![Page 47: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/47.jpg)
Lambda Calculus
13
The λ-calculus was developed by the logician Alonzo Church in 1930’s as a tool to study functions and computability.
2009年7月31日星期五
![Page 48: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/48.jpg)
λ Calculus
14
Equivalent to Turing MachinesTuring 1937
Equivalent to recursive functionsKleene 1936
(λ x. x + 2)
2009年7月31日星期五
![Page 49: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/49.jpg)
λ Calculus
14
Equivalent to Turing MachinesTuring 1937
Equivalent to recursive functionsKleene 1936
(λ x. x + 2) = (x) { return x + 2}
2009年7月31日星期五
![Page 50: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/50.jpg)
λ Calculus
14
Equivalent to Turing MachinesTuring 1937
Equivalent to recursive functionsKleene 1936
(λ x. x + 2)
(λ x. x + 2) 3 = 5
= (x) { return x + 2}
2009年7月31日星期五
![Page 51: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/51.jpg)
λ Calculus
14
Equivalent to Turing MachinesTuring 1937
Equivalent to recursive functionsKleene 1936
(λ f. f 3)(λ x. x+2) = ?
2009年7月31日星期五
![Page 52: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/52.jpg)
λ Calculus
14
Equivalent to Turing MachinesTuring 1937
Equivalent to recursive functionsKleene 1936
(λ f. f 3)(λ x. x+2) = ?
= (λ x. x + 2) 3= 3 + 2
2009年7月31日星期五
![Page 53: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/53.jpg)
High Order Function
15
• Functions take other functions as parameters
• Functions return functions as results
2009年7月31日星期五
![Page 54: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/54.jpg)
High Order Function
15
• Functions take other functions as parameters
• Functions return functions as results
∑ f(x) x∈L
2009年7月31日星期五
![Page 55: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/55.jpg)
High Order Function
15
• Functions take other functions as parameters
• Functions return functions as results d f(x)
dx = f’(x)
∑ f(x) x∈L
2009年7月31日星期五
![Page 56: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/56.jpg)
Mathematics
16
2009年7月31日星期五
![Page 57: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/57.jpg)
Mathematics
16
OK, no more mathematics.
2009年7月31日星期五
![Page 58: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/58.jpg)
Design Pattern
17
2009年7月31日星期五
![Page 59: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/59.jpg)
Design Pattern
17
Adapter Pattern
2009年7月31日星期五
![Page 60: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/60.jpg)
Design Pattern
17
Adapter Pattern
2009年7月31日星期五
![Page 61: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/61.jpg)
Design Pattern
17
Adapter Pattern
+
2009年7月31日星期五
![Page 62: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/62.jpg)
Design Pattern
17
Adapter Pattern
2009年7月31日星期五
![Page 63: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/63.jpg)
Design Pattern
17
Adapter Pattern
2009年7月31日星期五
![Page 64: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/64.jpg)
Currying
18
把接受多個參數的函數變換成接受一個單一參數(最初函數的第一個參數)的函數,並且返回接受餘下的參數而且返回結果的新函數的技術。
AJAX 的 callback function
2009年7月31日星期五
![Page 65: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/65.jpg)
No Side Effect
19
Output is solely dependent of input.
input outputPure Function
2009年7月31日星期五
![Page 66: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/66.jpg)
No Side Effect
19
Output is solely dependent of input.
input output
States
With Assignment
Impure Function
2009年7月31日星期五
![Page 67: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/67.jpg)
No Side Effect
19
Output is solely dependent of input.
input output
States
With Assignment
Impure Function
Can replace f(x) + f(x) with 2*f(x)?
2009年7月31日星期五
![Page 68: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/68.jpg)
Referential Transparent
20
If the function f is referential transparent.
2009年7月31日星期五
![Page 69: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/69.jpg)
Referential Transparent
20
If the function f is referential transparent.
What is this?
2009年7月31日星期五
![Page 70: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/70.jpg)
Referential Transparent
20
What is this?
If Function result depends only on the values of its parameters.
Definition:
2009年7月31日星期五
![Page 71: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/71.jpg)
Referential Transparent
20
What is this?
If Function result depends only on the values of its parameters.
Definition:
This concept occurs in mathematics, but it is broken by imperative programming languages.
Murmur:
2009年7月31日星期五
![Page 72: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/72.jpg)
Side Effect
21
Consider the following C/Java function f:
2009年7月31日星期五
![Page 73: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/73.jpg)
Side Effect
21
Consider the following C/Java function f:
f change the state outside the function.
2009年7月31日星期五
![Page 74: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/74.jpg)
Principle Of Least Surprise
22
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)
2009年7月31日星期五
![Page 75: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/75.jpg)
No Update On Variables
23
Variables are similar to Variables in mathematics, they hold a value but they can’t be updated.
Definition:
Pure
Variables only assigned once.
2009年7月31日星期五
![Page 76: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/76.jpg)
No Update On Variables
23
Variables are similar to Variables in mathematics, they hold a value but they can’t be updated.
Definition:
Pure
Variables only assigned once.
2009年7月31日星期五
![Page 77: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/77.jpg)
No Update On Variables
23
Variables are similar to Variables in mathematics, they hold a value but they can’t be updated.
Definition:
Pure
So...We don’t need to care call by value, call by reference...etc.
Variables only assigned once.
2009年7月31日星期五
![Page 78: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/78.jpg)
No Update On Variables
23
Variables are similar to Variables in mathematics, they hold a value but they can’t be updated.
Definition:
Pure
So...We don’t need to care call by value, call by reference...etc.
Variables only assigned once.
2009年7月31日星期五
![Page 79: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/79.jpg)
Recursion Basics
24
How to Loop?Functional programs are usually H .
(with map, fold, filter)
Pure
2009年7月31日星期五
![Page 80: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/80.jpg)
Recursion Basics
24
How to Loop?Functional programs are usually H .
(with map, fold, filter)eavily Recursive
Pure
2009年7月31日星期五
![Page 81: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/81.jpg)
Recursion Basics
24
How to Loop?Functional programs are usually H .
(with map, fold, filter)eavily Recursive
Iteration.isEqualTo( Recursive); Pure
2009年7月31日星期五
![Page 82: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/82.jpg)
Efficiency
25
It is true...Early:
Tail Recursion.Lazy Evaluation.MemoizationCommon Subexpression EliminationParallel Computing
Now:
2009年7月31日星期五
![Page 83: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/83.jpg)
Efficiency
25
It is true...Early:
Tail Recursion.Lazy Evaluation.MemoizationCommon Subexpression EliminationParallel Computing
Now:
2009年7月31日星期五
![Page 84: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/84.jpg)
Efficiency
25
It is true...Early:
Tail Recursion.Lazy Evaluation.MemoizationCommon Subexpression EliminationParallel Computing
Now:!=
2009年7月31日星期五
![Page 85: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/85.jpg)
Efficiency
25
It is true...Early:
Tail Recursion.Lazy Evaluation.MemoizationCommon Subexpression EliminationParallel Computing
Now:
2009年7月31日星期五
![Page 86: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/86.jpg)
Efficiency
25
It is true...Early:
Tail Recursion.Lazy Evaluation.MemoizationCommon Subexpression EliminationParallel Computing
Now:
2009年7月31日星期五
![Page 87: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/87.jpg)
Chat in Pugs Project
26
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 sec19: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 ok19: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. enjoy19:14 < malaire> that's quite fast development :)19:14 < autrijus> :)
2009年7月31日星期五
![Page 88: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/88.jpg)
Lazy Evaluation
27
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
2009年7月31日星期五
![Page 89: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/89.jpg)
Lazy Evaluation
27
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
2009年7月31日星期五
![Page 90: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/90.jpg)
Lazy Evaluation
27
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);
In Haskell, we don’t run S1 and S2 before we call Concatenate, we even don’t know which one evaluate first!
Pure
2009年7月31日星期五
![Page 91: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/91.jpg)
Lazy Evaluation
27
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);
In Haskell, we don’t run S1 and S2 before we call Concatenate, we even don’t know which one evaluate first!
Pure
Benefits:
2009年7月31日星期五
![Page 92: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/92.jpg)
Pattern Matching
28
A Good Picture Can Tell Every Thing!
2009年7月31日星期五
![Page 93: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/93.jpg)
Tail Recursion
29
2009年7月31日星期五
![Page 94: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/94.jpg)
Tail Recursion
29
fac 55 * (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
Stack
Time
2009年7月31日星期五
![Page 95: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/95.jpg)
Tail Recursion
29
2009年7月31日星期五
![Page 96: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/96.jpg)
Tail Recursion
29
fac 5tailfac 5 1tailfac 4 5 * 1tailfac 3 4 * 5 * 1tailfac 2 3 * 4 * 5 * 1tailfac 1 2 * 3 * 4 * 5 * 1tailfac 0 1 * 2 * 3 * 4 * 5 * 1..120
Stack
Time
2009年7月31日星期五
![Page 97: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/97.jpg)
Tail Recursion
29
fac 5tailfac 5 1tailfac 4 5 * 1tailfac 3 4 * 5 * 1tailfac 2 3 * 4 * 5 * 1tailfac 1 2 * 3 * 4 * 5 * 1tailfac 0 1 * 2 * 3 * 4 * 5 * 1..120
Stack
Time
5 14 53 202 601 1200 120
2009年7月31日星期五
![Page 98: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/98.jpg)
Tail Recursion
29
fac 5tailfac 5 1tailfac 4 5 * 1tailfac 3 4 * 5 * 1tailfac 2 3 * 4 * 5 * 1tailfac 1 2 * 3 * 4 * 5 * 1tailfac 0 1 * 2 * 3 * 4 * 5 * 1..120
Stack
Time
5 14 53 202 601 1200 120
Tail Recursion can be optimize to loop!!
2009年7月31日星期五
![Page 99: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/99.jpg)
Memoization
30
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.
2009年7月31日星期五
![Page 100: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/100.jpg)
Memoization
30
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.
2009年7月31日星期五
![Page 101: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/101.jpg)
Closure
31
In C
2009年7月31日星期五
![Page 102: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/102.jpg)
Closure
31
In C
New or Malloc.
2009年7月31日星期五
![Page 103: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/103.jpg)
Closure
31
In C
But Global is
2009年7月31日星期五
![Page 104: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/104.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
2009年7月31日星期五
![Page 105: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/105.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
I AmSymbol Table
GlobalI Am
Symbol Table
2009年7月31日星期五
![Page 106: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/106.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
I AmSymbol Table
GlobalI Am
Symbol Table
Local
2009年7月31日星期五
![Page 107: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/107.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
I AmSymbol Table
Global
2009年7月31日星期五
![Page 108: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/108.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
2009年7月31日星期五
![Page 109: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/109.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
2009年7月31日星期五
![Page 110: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/110.jpg)
Closure
31
In C
static and dynamic.Another issue: scope
2009年7月31日星期五
![Page 111: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/111.jpg)
Loop Helper
32
Not only Recursion, but also Loop Helper?
Recursion : divided and conquer
Loop : don’t do that so complicate
2009年7月31日星期五
![Page 112: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/112.jpg)
Folding
33
Alias : reduce
2009年7月31日星期五
![Page 113: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/113.jpg)
Folding
33
Alias : reduce
OperatorInitial valueList
2009年7月31日星期五
![Page 114: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/114.jpg)
Folding
33
Alias : reduce
2009年7月31日星期五
![Page 115: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/115.jpg)
Folding
33
Alias : reduce
2009年7月31日星期五
![Page 116: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/116.jpg)
Folding
33
Alias : reduce
2009年7月31日星期五
![Page 117: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/117.jpg)
Mapping
34
2009年7月31日星期五
![Page 118: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/118.jpg)
Mapping
34
2009年7月31日星期五
![Page 119: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/119.jpg)
Mapping
34
2009年7月31日星期五
![Page 120: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/120.jpg)
Mapping
34
Function f()a, b, c, ..., z f(a), f(b), f(c), ..., f(z)
2009年7月31日星期五
![Page 121: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/121.jpg)
Mapping
34
2009年7月31日星期五
![Page 122: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/122.jpg)
Filtering
35
Definition:
Function f()a, b, c, ..., z a’, b’, c’, ..., z’
if (p?) w, then send w to output
2009年7月31日星期五
![Page 123: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/123.jpg)
Filtering
35
Definition:
Function f()a, b, c, ..., z a’, b’, c’, ..., z’
if (p?) w, then send w to output
2009年7月31日星期五
![Page 124: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/124.jpg)
Quick Sort
36
2009年7月31日星期五
![Page 125: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/125.jpg)
Quick Sort
36
2009年7月31日星期五
![Page 126: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/126.jpg)
Quick Sort
36
2009年7月31日星期五
![Page 127: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/127.jpg)
Quick Sort
36
2009年7月31日星期五
![Page 128: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/128.jpg)
Insert Sort
37
2009年7月31日星期五
![Page 129: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/129.jpg)
Insert Sort
37
2009年7月31日星期五
![Page 130: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/130.jpg)
Insert Sort
37
2009年7月31日星期五
![Page 131: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/131.jpg)
Insert Sort
37
2009年7月31日星期五
![Page 132: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/132.jpg)
Insert Sort
37
2009年7月31日星期五
![Page 133: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/133.jpg)
Story
38
Paul GrahamDevelop Y! Store by Lisp.唐鳳Develop Perl6 by Haskell.蔡學鏞“學會 Lisp 會讓你生產力大增” by Beyond JavaMIT CS6.001 Structure and Interpretation of Computer Programs
2009年7月31日星期五
![Page 134: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/134.jpg)
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
2009年7月31日星期五
![Page 135: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/135.jpg)
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
2009年7月31日星期五
![Page 136: Functional Programming 之二三事](https://reader033.vdocuments.site/reader033/viewer/2022042512/54c208124a7959750b8b45b9/html5/thumbnails/136.jpg)
41
Question ?
2009年7月31日星期五