View
270
Download
0
Category
Tags:
Preview:
Citation preview
A Glance at FuncProgramming
Hu Zi Ming
hzmangel@gmail.com
Nov 28, 2009
1 / 20A Glance at FuncProgramming
N
Outline
1 What is FPSome StoriesFeaturesCompare with imperative Programming
2 Why Functional Programming
3 Demos and Applications
2 / 20A Glance at FuncProgramming
N
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
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
Feature of Functional Programming
Lazy evaluation
No assignment, no side effect
Closures and high-order functions
4 / 20A Glance at FuncProgramming
N
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
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
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
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
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
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
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
Outline
1 What is FP
2 Why Functional Programming
3 Demos and Applications
12 / 20A Glance at FuncProgramming
N
Why Functional Programming
No unit test
Easier for glueing
Faster for some building blocks
Easier for some algorithm
13 / 20A Glance at FuncProgramming
N
Outline
1 What is FP
2 Why Functional Programming
3 Demos and ApplicationsSome DemosApplications in Real World
14 / 20A Glance at FuncProgramming
N
Some Demons
Quick sort
Sum of prime list
Point 24
15 / 20A Glance at FuncProgramming
N
Quick Sort
Quick Sort
qsort [] = []
qsort (x:xs) = (qsort (filter (<x) xs)) ++ [x] ++ (qsort (filter (>=x) xs))
16 / 20A Glance at FuncProgramming
N
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
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
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
Q AND A
20 / 20A Glance at FuncProgramming
N
Recommended