33
Functional PROGRAMMING Zhongke Chen PayPal Risk Monday, June 24, 13

Introduction to Functional Programming

Embed Size (px)

Citation preview

Page 1: Introduction to Functional Programming

Functional PROGRAMMING

Zhongke ChenPayPal Risk

Monday, June 24, 13

Page 2: Introduction to Functional Programming

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

Why Learn FP?

Monday, June 24, 13

Page 4: Introduction to Functional Programming

FP is no longer exclusive to Lisp, Haskell, Erlang, ...

Monday, June 24, 13

Page 5: Introduction to Functional Programming

FP is no longer exclusive to Lisp, Haskell, Erlang, ...

Monday, June 24, 13

Page 6: Introduction to Functional Programming

)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))))))))) ))))))))))))))))))))))))))) ))))))))))))))))))))))))) )))))))))))))))))))))) )))))))))))))))))))) )))))))))))))))) ))))))))))))))) )))))))))))

Monday, June 24, 13

Page 7: Introduction to Functional Programming

How to learn Lisp - a new language?

Monday, June 24, 13

Page 8: Introduction to Functional Programming

Primitive ElementsMeans of CombinationMeans of Abstraction

Monday, June 24, 13

Page 9: Introduction to Functional Programming

• 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

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

(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

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

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

fibs = 0:1:zipWith (+) fibs (tail fibs)

Infinite List (needs Lazy Evaluation)

Monday, June 24, 13

Page 15: Introduction to Functional Programming

More example:

fact(n) = n!

Monday, June 24, 13

Page 16: Introduction to Functional Programming

(DEFINE (FACT n)

(IF (= n 0)

1

(* n (FACT (- n 1))))) <- Recursion

Monday, June 24, 13

Page 17: Introduction to Functional Programming

(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

(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

(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

Final Example

sqrt(x)

Monday, June 24, 13

Page 21: Introduction to Functional Programming

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

(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

(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

Then how to calculate Cube root etc?

We need higher level of abstraction!

Monday, June 24, 13

Page 25: Introduction to Functional Programming

Fixed-Point of function f:

is the x that f(x) = x

Monday, June 24, 13

Page 26: Introduction to Functional Programming

Given fixed-point, how to calculate sqrt(x)?

f(y) = (y+x/y)/2

Monday, June 24, 13

Page 27: Introduction to Functional Programming

(DEFINE (SQRT x)

(FIXED-POINT

(LAMBDA (y)

(AVERAGE (/ x y) y))

1))

Monday, June 24, 13

Page 28: Introduction to Functional Programming

(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

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

(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

(DEFINE (SQRT x)

(NEWTON

(lambda (y) (- (square y) x))

1.0))

Monday, June 24, 13

Page 32: Introduction to Functional Programming

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

Thanks!

Monday, June 24, 13