20091128 func prog

346 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
346
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
2
Comments
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. Outline1 What is FP Some Stories Features Compare with imperative Programming2 Why Functional Programming3 Demos and Applications A Glance at FuncProgramming 2 / 20
  3. 3. Two models of computationIdeal 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 computationIdeal 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 languageHow 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 ProgrammingLazy evaluationNo assignment, no side effectClosures and high-order functions A Glance at FuncProgramming 4 / 20
  6. 6. Lazy EvaluationDelaying computation until the result is requiredAvoiding unnecessary calculationsBe able to construct infinite data structuresMinimal computation A Glance at FuncProgramming 5 / 20
  7. 7. Snippets of LazyLazy in c/c++/java/. . .if a then b else cLazy in pythondef lazy_fib ( foo_list = [1 , 1]): while True : yield foo_list foo_list . append ( foo_list [ -2] + foo_list [ -1])Lazy in haskellfibs = 0 : 1 : zipWith (+) fibs ( tail fibs ) A Glance at FuncProgramming 6 / 20
  8. 8. No Assignment, No Side EffectsNo destructive update (x = x + 1)Referential Transparent: output ONLY depends on inputState change: assign only ONCE make no state changeGood for debuging/referactoring/parallel. . . A Glance at FuncProgramming 7 / 20
  9. 9. List of Side Effects in FunctionsVariable assignment (static, global, . . . )Change arguments passed inRaise exceptionsWrite data to display or fileCall other side-effecting functions (malloc, fwrite, . . . ) A Glance at FuncProgramming 8 / 20
  10. 10. Closures and Higher-order FunctionsFirst-order functions and bounded variables make closureCan be used for lambda functionCan be used to construct higher-order functionsGood for modularization A Glance at FuncProgramming 9 / 20
  11. 11. Snippets for Closures and High-order Functionsfilter/map/reduce in pythonfilter ( lambda x : x % 2 == 0 , range (30)) # even numbersmap ( lambda x : x ** 2 , range (30)) # powerreduce ( lambda x , y : x * y , range (30)) # factorial A Glance at FuncProgramming 10 / 20
  12. 12. Compare with Imperative ProgrammingFunction is a first-class type in FPSolving problems: IP concern on how while FP on whatMost functions in FP are state-lessThe position of invoking may effect the result of functions in IPIP use loop/condition for flow control while recursion in FPIP is instruction-oriented while FP is function/data-oriented A Glance at FuncProgramming 11 / 20
  13. 13. Outline1 What is FP2 Why Functional Programming3 Demos and Applications A Glance at FuncProgramming 12 / 20
  14. 14. Why Functional ProgrammingNo unit testEasier for glueingFaster for some building blocksEasier for some algorithm A Glance at FuncProgramming 13 / 20
  15. 15. Outline1 What is FP2 Why Functional Programming3 Demos and Applications Some Demos Applications in Real World A Glance at FuncProgramming 14 / 20
  16. 16. Some DemonsQuick sortSum of prime listPoint 24 A Glance at FuncProgramming 15 / 20
  17. 17. Quick SortQuick Sortqsort [] = []qsort ( x : xs ) = ( qsort ( filter ( < x ) xs )) ++ [ x ] ++ ( qsort ( filter ( >= x ) xs )) A Glance at FuncProgramming 16 / 20
  18. 18. Sum of Prime ListSieve of Eratosthenessieve ( 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 Methodis_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 2424 Pointsops1 = [(+) , ( -) , (*) , (/)]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 Worldelisp in emacsmathmaticsErlang in web serverlist in AI related areaML in compiler related area A Glance at FuncProgramming 19 / 20
  21. 21. Q AND A A Glance at FuncProgramming 20 / 20

×