Upcoming SlideShare
×

# 20091128 func prog

907 views

Published on

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
907
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
7
0
Likes
0
Embeds 0
No embeds

No notes for slide

### 20091128 func prog

1. 1. A Glance at FuncProgramming Hu Zi Ming hzmangel@gmail.com Nov 28, 2009 A Glance at FuncProgramming 1 / 20
2. 2. Outline 1 What is FP Some Stories Features Compare with imperative Programming 2 Why Functional Programming 3 Demos and Applications A Glance at FuncProgramming 2 / 20
3. 3. Two models of computation Ideal from Leibniz Create a universal language in which all possible problems can be stated Find a decision method to solve all the problems stated in the universal language A Glance at FuncProgramming 3 / 20
4. 4. Two models of computation Ideal from Leibniz Create a universal language in which all possible problems can be stated Find a decision method to solve all the problems stated in the universal language How to solve a computable problem Turing machine by Alan Turing Lambda calculus by Alonzo Church A Glance at FuncProgramming 3 / 20
5. 5. Feature of Functional Programming Lazy evaluation No assignment, no side eﬀect Closures and high-order functions A Glance at FuncProgramming 4 / 20
6. 6. Lazy Evaluation Delaying computation until the result is required Avoiding unnecessary calculations Be able to construct inﬁnite data structures Minimal computation A Glance at FuncProgramming 5 / 20
7. 7. 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 ) A Glance at FuncProgramming 6 / 20
8. 8. No Assignment, No Side Eﬀects 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. . . A Glance at FuncProgramming 7 / 20
9. 9. List of Side Eﬀects in Functions Variable assignment (static, global, . . . ) Change arguments passed in Raise exceptions Write data to display or ﬁle Call other side-eﬀecting functions (malloc, fwrite, . . . ) A Glance at FuncProgramming 8 / 20
10. 10. 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 A Glance at FuncProgramming 9 / 20
11. 11. Snippets for Closures and High-order Functions ﬁlter/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 A Glance at FuncProgramming 10 / 20
12. 12. Compare with Imperative Programming Function is a ﬁrst-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 eﬀect the result of functions in IP IP use loop/condition for ﬂow control while recursion in FP IP is instruction-oriented while FP is function/data-oriented A Glance at FuncProgramming 11 / 20
13. 13. Outline 1 What is FP 2 Why Functional Programming 3 Demos and Applications A Glance at FuncProgramming 12 / 20
14. 14. Why Functional Programming No unit test Easier for glueing Faster for some building blocks Easier for some algorithm A Glance at FuncProgramming 13 / 20
15. 15. Outline 1 What is FP 2 Why Functional Programming 3 Demos and Applications Some Demos Applications in Real World A Glance at FuncProgramming 14 / 20
16. 16. Some Demons Quick sort Sum of prime list Point 24 A Glance at FuncProgramming 15 / 20
17. 17. Quick Sort Quick Sort qsort [] = [] qsort ( x : xs ) = ( qsort ( filter ( < x ) xs )) ++ [ x ] ++ ( qsort ( filter ( >= x ) xs )) A Glance at FuncProgramming 16 / 20
18. 18. Sum of Prime List Sieve of Eratosthenes sieve ( x : xs ) = x : sieve [ n | n < - xs , n ‘ mod ‘ x /=0] p r i m e _ l ist_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) A Glance at FuncProgramming 17 / 20
19. 19. 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] A Glance at FuncProgramming 18 / 20
20. 20. Applications in Real World elisp in emacs mathmatics Erlang in web server list in AI related area ML in compiler related area A Glance at FuncProgramming 19 / 20
21. 21. Q AND A A Glance at FuncProgramming 20 / 20