Introduction to Functional Programming
-
Upload
zhongke-chen -
Category
Technology
-
view
265 -
download
1
Transcript of Introduction to Functional Programming
![Page 1: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/1.jpg)
Functional PROGRAMMING
Zhongke ChenPayPal Risk
Monday, June 24, 13
![Page 2: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/2.jpg)
My name’s Zhongke Chen(陈忠克)
Python, C/C++ Coder
I worked for Wenzhou University, Virtuos Games, Ericsson
I majored in EE (Multimedia Analysis)
Twitter, Sina Weibo: @ch3n2k
Monday, June 24, 13
![Page 3: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/3.jpg)
Why Learn FP?
Monday, June 24, 13
![Page 4: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/4.jpg)
FP is no longer exclusive to Lisp, Haskell, Erlang, ...
Monday, June 24, 13
![Page 5: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/5.jpg)
FP is no longer exclusive to Lisp, Haskell, Erlang, ...
Monday, June 24, 13
![Page 6: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/6.jpg)
)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))) ))))))))))))))))))))))))) )))))))))))))))))))))) )))))))))))))))))))) )))))))))))))))) ))))))))))))))) )))))))))))
Monday, June 24, 13
![Page 7: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/7.jpg)
How to learn Lisp - a new language?
Monday, June 24, 13
![Page 8: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/8.jpg)
Primitive ElementsMeans of CombinationMeans of Abstraction
Monday, June 24, 13
![Page 9: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/9.jpg)
• Primitives
+ * 4 3.14
• Combinations
(+ 4 3.14 2.6)
(* (+ 4 4) (+ 5 5))
(IF (5 > 4) 5 4)
• Abstractions
(DEFINE A (* 5 5))
(DEFINE SQUARE (LAMBDA (x) (* x x))) <- Lambda
(DEFINE (SQAURE x) (* x x)) <- Syntactic Sugar
Monday, June 24, 13
![Page 10: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/10.jpg)
Exercise:
1. average (x, y) = (x+y)/2
2. mean-square(x,y) = (x*x + y*y)/2
3. abs(x) = |x|
Monday, June 24, 13
![Page 11: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/11.jpg)
(DEFINE (AVERAGE x y)
(/ (+ x y) 2))
(DEFINE (MEAN-SQUARE x y)
(AVERAGE (SQUARE x)
(SQUARE y)))
(DEFINE (ABS x)
(IF (< x 0)
(- x)
x))
Monday, June 24, 13
![Page 12: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/12.jpg)
Evaluation Order of MEAN-SQUARE(4, 6)?
MEAN-SUQARE(4, 6)
-> (AVERAGE (SQUARE 4) (SQUARE 6))
-> (AVERAGE 16 (SQUARE 6))
-> (AVERAGE 16 36)
-> 26
Strict Order
Monday, June 24, 13
![Page 13: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/13.jpg)
MEAN-SUQARE(4, 6)
-> (AVERAGE (SQUARE 4) (SQUARE 6))
-> (/ (+ (SQUARE 4) (SQUARE 6)) 2)
-> (/ (+ (SQUARE 4) (SQUARE 6)) 2)
-> (/ (+ (* 4 4) (SQUARE 6)) 2)
-> (/ (+ 16 (SQUARE 6)) 2)
...
Non-Strict Order (Lazy Evaluation)Monday, June 24, 13
![Page 14: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/14.jpg)
fibs = 0:1:zipWith (+) fibs (tail fibs)
Infinite List (needs Lazy Evaluation)
Monday, June 24, 13
![Page 15: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/15.jpg)
More example:
fact(n) = n!
Monday, June 24, 13
![Page 16: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/16.jpg)
(DEFINE (FACT n)
(IF (= n 0)
1
(* n (FACT (- n 1))))) <- Recursion
Monday, June 24, 13
![Page 17: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/17.jpg)
(FACT 3)
->(IF (= 3 0) 1 (* 3 (FACT (- 3 1))))
->(* 3 (FACT (- 3 1)))
->(* 3 (FACT 2))
->(* 3 (IF (= 2 0) 1 (* 2 (FACT (- 2 1)))))
->(* 3 (* 2 (FACT 1)))
->(* 3 (* 2 (* 1 (FACT 0))))
->(* 3 (* 2 (* 1 1)))
->(* 3 (* 2 1))
->(* 3 2)
->6
Monday, June 24, 13
![Page 18: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/18.jpg)
(DEFINE (FACT-IMPL n r)
(IF (= n 0)
r
(FACT-IMPL (- n 1) (* n r))))
(DEFINE (FACT n)
(FACT-IMPL n 1))
Tail Call Optimization
Monday, June 24, 13
![Page 19: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/19.jpg)
(FACT 3)
->(FACT-IMPL 3 1)
->(IF (= 3 0) 1 (FACT-IMPL (- 3 1) (* 3 1)))
->(FACT-IMPL 2 3)
->(IF (= 2 0) 3 (FACT-IMPL (- 2 1) (* 2 3)))
->(FACT-IMPL 1 6)
->(IF (= 1 0) 6 (FACT-IMPL (- 1 1) (* 1 6)))
->(FACT-IMPL 0 6)
->(IF (= 0 0) 6 (FACT-IMPL (- 0 1) (* 0 6)))
->6
Monday, June 24, 13
![Page 20: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/20.jpg)
Final Example
sqrt(x)
Monday, June 24, 13
![Page 21: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/21.jpg)
Algorithm
make a guess G
improve the guess by averaging G and x/G
keep improving the guess until it’s good enough
use 1 as an initial guess
Monday, June 24, 13
![Page 22: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/22.jpg)
(DEFINE (TRY guess x)
(IF (GOOD-ENOUGH? guess x)
guess
(TRY (IMPROVE guess x) x)))
(DEFINE (SQRT x) (TRY 1 x))
(DEFINE (IMPROVE guess x)
(AVERAGE guess (/ x guess)))
(DEFINE (GOOD-ENOUGH? guess x)
(< (ABS (- (SQUARE guess) x))
0.00001))
Monday, June 24, 13
![Page 23: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/23.jpg)
(DEFINE (SQRT x)
(DEFINE (IMPROVE guess) <- Closure
(AVERAGE guess (/ x guess)))
(DEFINE (GOOD-ENOUGH? guess) <- Closure
(< (ABS (- (SQUARE guess) x))
0.00001))
(DEFINE (TRY guess)
(IF (GOOD-ENOUGH? guess)
guess
(TRY (IMPROVE guess))))
(TRY 1))
Monday, June 24, 13
![Page 24: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/24.jpg)
Then how to calculate Cube root etc?
We need higher level of abstraction!
Monday, June 24, 13
![Page 25: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/25.jpg)
Fixed-Point of function f:
is the x that f(x) = x
Monday, June 24, 13
![Page 26: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/26.jpg)
Given fixed-point, how to calculate sqrt(x)?
f(y) = (y+x/y)/2
Monday, June 24, 13
![Page 27: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/27.jpg)
(DEFINE (SQRT x)
(FIXED-POINT
(LAMBDA (y)
(AVERAGE (/ x y) y))
1))
Monday, June 24, 13
![Page 28: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/28.jpg)
(DEFINE (FIXED-POINT f guess)
(DEFINE (CLOSE-ENOUGH? u v)
(< (ABS (- u v)) 0.0001))
(DEFINE (ITER OLD NEW)
(IF (CLOSE-ENOUGH? OLD NEW)
NEW
(ITER NEW (f NEW))))
(ITER guess (f guess)))
Higher Order Function
Monday, June 24, 13
![Page 29: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/29.jpg)
Newton’s method converges more rapidly
find a y that f(y) = 0
start a guess y0,
yn+1 = yn - f(yn)/f’(yn)
Monday, June 24, 13
![Page 30: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/30.jpg)
(DEFINE (NEWTON f guess)
(DEFINE DF (DERIV f))
(FIXED-POINT
(LAMBDA (y) (- y (/ (f y)(DF y))))
guess))
(DEFINE DERIV
(LAMBDA (f)
(LAMBDA (x)
(/ (- (f (+ x dx))(f x)) dx))))
(DEFINE dx 0.00001)
Monday, June 24, 13
![Page 31: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/31.jpg)
(DEFINE (SQRT x)
(NEWTON
(lambda (y) (- (square y) x))
1.0))
Monday, June 24, 13
![Page 32: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/32.jpg)
Covered:
Lambda
Tail Call Optimization
Lazy Evaluation
Closure
Higher Order Function
Not Covered:
Currying
Continuation
Pattern Matching
Monads
...
Monday, June 24, 13
![Page 33: Introduction to Functional Programming](https://reader034.fdocuments.net/reader034/viewer/2022042716/55a6cb071a28ab531d8b4925/html5/thumbnails/33.jpg)
Thanks!
Monday, June 24, 13