ihome inaction 篇外篇之fp介绍

Download Ihome inaction 篇外篇之fp介绍

If you can't read please download the document

Upload: dennis-zhuang

Post on 19-May-2015

870 views

Category:

Technology


0 download

DESCRIPTION

很早以前做的一次函数式编程的介绍,很不成熟,做个备份。

TRANSCRIPT

  • 1. Ihome In Action Author:Dennis IHOME

2. FP

  • FP Functional Programming
  • lambda
  • Lambda
  • Alonzo Church Stephen Cole Kleene 20 30

3. FP

  • Lambda
  • 1 Erlang
  • 2
  • amb
  • Less Code

4. FP

  • currying Continuation
  • Mond

5.

  • 1 final
  • 2 erlang
  • 3

6.

  • scheme procedure
  • +=>#
  • (define (square x) (* x x)
  • (square 3)=> 9
  • (square 4)=> 10
  • ( (lambda(x) (* x x))3)=> 9
  • high-order function FP first-class

7.

  • Map foreach filter
  • map, proc
  • (define (map proc items)
  • (if (null? Items)
  • '()
  • (cons (proc (car items))
  • (map proc (cdr items)))))
  • (map square (list 1 2 3 4))
  • => (1 4 9 16)

8. filter

  • ,predicate
  • (define (filter predicate sequence)
  • (cond ((null? sequence) '())
  • ((predicate (car sequence))
  • (cons (car sequence) (filter predicate (cdr sequence))))
  • (else
  • (filter predicate (cdr sequence)))))

9. accumulate

  • OP
  • (define (accumulate op initial sequence)
  • (if (null? sequence)
  • initial
  • (op (car sequence) (accumulate op initial (cdr sequence)))))

10.

  • ,
  • low high)
  • '()
  • (cons low (enumerate-interval (+ low 1) high))))
  • (enumerate-interval 0 n)

11.

  • map0-nfib(k)
  • (map fib (enumerate-interval 0 n))
  • (filter even?
  • (map fib (enumerate-interval 0 n)))

12.

  • (accumulate cons
  • '()
  • (filter even?
  • (map (enumerate-interval 0 n)))))

13.

  • (define (even-fibs n)
  • (accumulate cons
  • '()
  • (filter even?
  • (map fib (enumerate-interval 0 n)))))

14.

15. -

  • block
  • (define (get-adder x)
  • (lambda(y)(+ x y)))
  • X get-adder
  • (define add4 (get-adder 4))
  • (add4 7)=> 11

16. -

  • Java:
  • class Product implements Comparator{
  • public int compare(Product other)
  • return this.price-other.price
  • Collections.sort(products)
  • Ruby:
  • products.sort{|a,b| a.price-b.price}

17. -

  • Java7
  • double log = { double x => Math.log(x) }.invoke(10);
  • int sum = { int x, int y => x + y }.invoke(3, 4); // will return 7
  • String[] girls = { "Jane", "Eva", "Sarah", "Alice" };
  • Arrays.sort(girls, { String s1, String s2 => int r = s1.length() - s2.length(); r == 0 ? s1.compareTo(s2) : r });

18.

  • lua:
  • function make_stack()
  • local data = {}; local last = -1;
  • local function push(e) last = last + 1; data[last] = e; end
  • local function pop() if last == -1 then return nil end last = last - 1 return data[last+1] end
  • return function (index) local tb = {push=push, pop=pop} return tb[index] end end
  • s=make_stack()
  • s("push")("test0")
  • s("push")("test1") s("push")("test2") s("push")("test3")
  • print(s("pop")()) print(s("pop")()) print(s("pop")())

19. Currying

  • Currying Haskell Curry lambda curry

20. Currying 1

  • Javascript
  • functionadd(x,y) {if(x!=null&&y!=null)returnx+y;
  • elseif(x!=null&&y==null)
  • returnfunction(y) {returnx+y;}
  • elseif(x==null&&y!=null)
  • returnfunction(x){returnx+y;}}
  • vara=add(3,4);
  • varb=add(2);
  • varc=b(10);

21. Currying 2

  • Ruby1.9 Proc#curry
  • plus=lambda{|a,b|a+b}
  • plus(3,4)=>7
  • curried_plus=plus.curry
  • plus_three=curried_plus[3]
  • plus_three(4)=>7

22. Currying 3

  • is_weekday=lambda{|day_of_week,time|time.wday==day_of_week}.curry
  • sunday=is_weekday[0]
  • monday=is_weekday[1]
  • tuesday=is_weekday[2]
  • wednesday=is_weekday[3]
  • thursday=is_weekday[4]
  • friday=is_weekday[5]
  • saturday=is_weekday[6]
  • caseTime.now
  • whensunday
  • puts"Dayofrest"
  • whenmonday,tuesday,wednesday,thursday,friday
  • puts"Work"
  • whensaturday
  • puts"chores"
  • end

23.

  • Lazy evalution
  • 1
  • 2 (stream)

24.

  • Unless
  • (define (unless condition usual-value exception-value)
  • (if condition
  • Exception-value
  • Usual-value))
  • (unless (= 0 0)
  • (/ 1 0)
  • (begin (display exception return 0) 0))

25.

  • (/ 1 0)
  • (= 0 0)trueexception-value(/ 1 0)

26.

  • conscarcdrstream-cons stream-car stream-cdrstream-map stream-filter
  • :(define (integers-starting-from n)
  • (cons-stream n (integers-starting-from (+ n1))))
  • (define integers (integers-starting-from 1))

27.

  • 1
  • (define ones (cons-stream 1 ones))

28.

  • (define (divisible? x y) (= (remainder x y) 0))
  • (define (sieve stream)
  • (cons-stream
  • (stream-car stream)
  • (sieve (stream-filter (lambda(x)
  • (not (divisible? x (stream-car stream))))
  • (stream-cdr stream)))))
  • (define primes (sieve (integers-starting-from 2)))

29. Continuation

  • 1 what to evalute?
  • 2 waht to do with the value?
  • what to do with the value (continuation)

30. Continuation

  • (if (null? x)
  • '()
  • (cdr x))
  • (null? x)'()(cdr x)(null? x).
  • (+ 2
  • (call/cc
  • (lambda (k)
  • (* 5 (k 4)))))=> 6

31. Continuation

  • call/cc:call-with-current-continuation
  • continuation
  • SchemeRubycall/cc
  • Continuaton
  • 1breakgoto
  • 2
  • 3failtry

32. Continuation break

  • schemebreakcontinuation
  • (define product
  • (lambda (ls)
  • (call/cc
  • (lambda (break)
  • (let f ((ls ls))
  • (cond
  • ((null? ls) 1)
  • ((= (car ls) 0) (break 0))
  • (else (* (car ls) (f (cdr ls))))))))))

33. Continuation amb

  • amb, John McCarthy 1961
  • Amb
  • amb e1 e2 e3 e4 ...)
  • n ei
  • (list (amb 1 2 3) (amb 'a 'b))
  • (1 a) (1 b)(2 a) (2 b)(3 a) (3 b)
  • (amb) fail
  • (define (require p)
  • (if (not p) (amb)))

34. amb

  • call/ccambcontinuation
  • scheme continuation 2.scm
  • Rubycontinuation.rb

35. amb

  • :
  • Baker, Cooper, Fletcher, Miller, Smith5BakerCooperFletcherMillerCooperSmithFletcherFletcherCooper

36.