20091128 func prog
TRANSCRIPT
![Page 1: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/1.jpg)
A Glance at FuncProgramming
Hu Zi Ming
Nov 28, 2009
1 / 20A Glance at FuncProgramming
N
![Page 2: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/2.jpg)
Outline
1 What is FPSome StoriesFeaturesCompare with imperative Programming
2 Why Functional Programming
3 Demos and Applications
2 / 20A Glance at FuncProgramming
N
![Page 3: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/3.jpg)
Two models of computation
Ideal from Leibniz
Create a universal language in which all possible problems can bestatedFind a decision method to solve all the problems stated in theuniversal language
How to solve a computable problem
Turing machine by Alan TuringLambda calculus by Alonzo Church
3 / 20A Glance at FuncProgramming
N
![Page 4: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/4.jpg)
Two models of computation
Ideal from Leibniz
Create a universal language in which all possible problems can bestatedFind a decision method to solve all the problems stated in theuniversal language
How to solve a computable problem
Turing machine by Alan TuringLambda calculus by Alonzo Church
3 / 20A Glance at FuncProgramming
N
![Page 5: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/5.jpg)
Feature of Functional Programming
Lazy evaluation
No assignment, no side effect
Closures and high-order functions
4 / 20A Glance at FuncProgramming
N
![Page 6: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/6.jpg)
Lazy Evaluation
Delaying computation until the result is required
Avoiding unnecessary calculations
Be able to construct infinite data structures
Minimal computation
5 / 20A Glance at FuncProgramming
N
![Page 7: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/7.jpg)
Snippets of Lazy
Lazy in c/c++/java/. . .
if a then b else c
Lazy in python
def lazy_fib(foo_list = [1, 1]):
while True:
yield foo_list
foo_list.append(foo_list [-2] + foo_list [-1])
Lazy in haskell
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
6 / 20A Glance at FuncProgramming
N
![Page 8: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/8.jpg)
No Assignment, No Side Effects
No destructive update (x = x + 1)
Referential Transparent: output ONLY depends on input
State change: assign only ONCE make no state change
Good for debuging/referactoring/parallel. . .
7 / 20A Glance at FuncProgramming
N
![Page 9: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/9.jpg)
List of Side Effects in Functions
Variable assignment (static, global, . . . )
Change arguments passed in
Raise exceptions
Write data to display or file
Call other side-effecting functions (malloc, fwrite, . . . )
8 / 20A Glance at FuncProgramming
N
![Page 10: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/10.jpg)
Closures and Higher-order Functions
First-order functions and bounded variables make closure
Can be used for lambda function
Can be used to construct higher-order functions
Good for modularization
9 / 20A Glance at FuncProgramming
N
![Page 11: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/11.jpg)
Snippets for Closures and High-orderFunctions
filter/map/reduce in python
filter(lambda x: x % 2 == 0, range (30)) # even numbers
map(lambda x: x ** 2, range (30)) # power
reduce(lambda x, y: x * y, range (30)) # factorial
10 / 20A Glance at FuncProgramming
N
![Page 12: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/12.jpg)
Compare with Imperative Programming
Function is a first-class type in FP
Solving problems: IP concern on how while FP on what
Most functions in FP are state-less
The position of invoking may effect the result of functions in IP
IP use loop/condition for flow control while recursion in FP
IP is instruction-oriented while FP is function/data-oriented
11 / 20A Glance at FuncProgramming
N
![Page 13: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/13.jpg)
Outline
1 What is FP
2 Why Functional Programming
3 Demos and Applications
12 / 20A Glance at FuncProgramming
N
![Page 14: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/14.jpg)
Why Functional Programming
No unit test
Easier for glueing
Faster for some building blocks
Easier for some algorithm
13 / 20A Glance at FuncProgramming
N
![Page 15: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/15.jpg)
Outline
1 What is FP
2 Why Functional Programming
3 Demos and ApplicationsSome DemosApplications in Real World
14 / 20A Glance at FuncProgramming
N
![Page 16: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/16.jpg)
Some Demons
Quick sort
Sum of prime list
Point 24
15 / 20A Glance at FuncProgramming
N
![Page 17: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/17.jpg)
Quick Sort
Quick Sort
qsort [] = []
qsort (x:xs) = (qsort (filter (<x) xs)) ++ [x] ++ (qsort (filter (>=x) xs))
16 / 20A Glance at FuncProgramming
N
![Page 18: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/18.jpg)
Sum of Prime List
Sieve of Eratosthenes
sieve (x:xs) = x : sieve [n | n<-xs, n ‘\mod ‘ x /=0]
prime_list_sieve n = sieve [2..n]
main = sum (prime_list_sieve 2000)
Another Method
is_prime x = x > 1 && (all (\n -> x ‘mod ‘ n /= 0 )
$ takeWhile (\n -> n*n <= x) [2..])
prime_list_is n = filter is_prime [2..n]
main = sum (prime_list_is 2000)
17 / 20A Glance at FuncProgramming
N
![Page 19: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/19.jpg)
Point 24
24 Points
ops1 = [(+), (-), (*), (/)]
ops2 = [(-), (/)]
listall [a, b] = [op a b | op <- ops1] ++ [op b a | op <- ops2]
listall (x:xs) = [op x y | op <- ops1 , y <- nub (listall xs)]
++ [op x y | op <- ops2 , y <- nub (listall xs)]
p24 x = elem 24 (listall x)
main = p24 [1,2,3,4]
18 / 20A Glance at FuncProgramming
N
![Page 20: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/20.jpg)
Applications in Real World
elisp in emacs
mathmatics
Erlang in web server
list in AI related area
ML in compiler related area
19 / 20A Glance at FuncProgramming
N
![Page 21: 20091128 func prog](https://reader033.vdocument.in/reader033/viewer/2022052823/55514688b4c905f2288b4d53/html5/thumbnails/21.jpg)
Q AND A
20 / 20A Glance at FuncProgramming
N