- 1. Ihome In Action Author:Dennis IHOME
2. FP
- FP Functional Programming
- Alonzo Church Stephen Cole Kleene 20 30
3. FP
4. FP
5.
6.
- (define (square x) (* x x)
- ( (lambda(x) (* x x))3)=> 9
- high-order function FP first-class
7.
- (map proc (cdr items)))))
- (map square (list 1 2 3 4))
8. filter
- (define (filter predicate sequence)
- (cond ((null? sequence) '())
- ((predicate (car sequence))
- (cons (car sequence) (filter predicate (cdr sequence))))
- (filter predicate (cdr sequence)))))
9. accumulate
- (define (accumulate op initial sequence)
- (op (car sequence) (accumulate op initial (cdr
sequence)))))
10.
- (cons low (enumerate-interval (+ low 1) high))))
11.
- (map fib (enumerate-interval 0 n))
- (map fib (enumerate-interval 0 n)))
12.
- (map (enumerate-interval 0 n)))))
13.
- (map fib (enumerate-interval 0 n)))))
14.
15. -
- (define add4 (get-adder 4))
16. -
- class Product implements Comparator{
- public int compare(Product other)
- return this.price-other.price
- Collections.sort(products)
- products.sort{|a,b| a.price-b.price}
17. -
- 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.
- 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("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
- functionadd(x,y) {if(x!=null&&y!=null)returnx+y;
- returnfunction(y) {returnx+y;}
- returnfunction(x){returnx+y;}}
21. Currying 2
- plus_three=curried_plus[3]
22. Currying 3
-
is_weekday=lambda{|day_of_week,time|time.wday==day_of_week}.curry
- whenmonday,tuesday,wednesday,thursday,friday
23.
24.
- (define (unless condition usual-value exception-value)
- (begin (display exception return 0) 0))
25.
- (= 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.
- (define ones (cons-stream 1 ones))
28.
- (define (divisible? x y) (= (remainder x y) 0))
- (sieve (stream-filter (lambda(x)
- (not (divisible? x (stream-car stream))))
- (define primes (sieve (integers-starting-from 2)))
29. Continuation
- 2 waht to do with the value?
- what to do with the value (continuation)
30. Continuation
- (null? x)'()(cdr x)(null? x).
31. Continuation
- call/cc:call-with-current-continuation
32. Continuation break
- ((= (car ls) 0) (break 0))
- (else (* (car ls) (f (cdr ls))))))))))
33. Continuation amb
- (list (amb 1 2 3) (amb 'a 'b))
- (1 a) (1 b)(2 a) (2 b)(3 a) (3 b)
34. amb
- scheme continuation 2.scm
35. amb
- Baker, Cooper, Fletcher, Miller,
Smith5BakerCooperFletcherMillerCooperSmithFletcherFletcherCooper
36.