20091128 func prog
Upcoming SlideShare
Loading in...5
×
 

20091128 func prog

on

  • 779 views

 

Statistics

Views

Total Views
779
Views on SlideShare
777
Embed Views
2

Actions

Likes
0
Downloads
5
Comments
0

1 Embed 2

http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    20091128 func prog 20091128 func prog Presentation Transcript

    • A Glance at FuncProgramming Hu Zi Ming hzmangel@gmail.com Nov 28, 2009 A Glance at FuncProgramming 1 / 20
    • 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
    • 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
    • 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
    • Feature of Functional Programming Lazy evaluation No assignment, no side effect Closures and high-order functions A Glance at FuncProgramming 4 / 20
    • Lazy Evaluation Delaying computation until the result is required Avoiding unnecessary calculations Be able to construct infinite data structures Minimal computation A Glance at FuncProgramming 5 / 20
    • 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
    • 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. . . A Glance at FuncProgramming 7 / 20
    • 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, . . . ) A Glance at FuncProgramming 8 / 20
    • 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
    • Snippets for Closures and High-order Functions 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 A Glance at FuncProgramming 10 / 20
    • 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 A Glance at FuncProgramming 11 / 20
    • Outline 1 What is FP 2 Why Functional Programming 3 Demos and Applications A Glance at FuncProgramming 12 / 20
    • Why Functional Programming No unit test Easier for glueing Faster for some building blocks Easier for some algorithm A Glance at FuncProgramming 13 / 20
    • 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
    • Some Demons Quick sort Sum of prime list Point 24 A Glance at FuncProgramming 15 / 20
    • Quick Sort Quick Sort qsort [] = [] qsort ( x : xs ) = ( qsort ( filter ( < x ) xs )) ++ [ x ] ++ ( qsort ( filter ( >= x ) xs )) A Glance at FuncProgramming 16 / 20
    • 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
    • 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
    • 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
    • Q AND A A Glance at FuncProgramming 20 / 20