Requirements
• Common Knowledge of Programming• Maybe Python/Ruby• Recursion
LAMBDA INTRO
KIM LEO
λ
lambda expression
def square(x):return x*x
=> square = lambda x: x*x
e.g.:(lambda x: x*x)(8) =>8*8 =>64
But how could make use of LAMBDA?
lambda expression
alist: [8, 5, 2, 4, 62, 4, 6]anOp: blablablaapply the operation to the list.
like:for i = 0 to len(alist):
anOp( alist[i] )or:foreach i in alist:
anOp( i )
Implement
apply_square_for_each(alist):foreach i in alist: square( i );
apply_plusplus7_for_each(alist):foreach i in alist: plusplus7( i );
apply_for_each(alist, op):case
when op == "square":foreach i in alist: square( i )
.....
lambda expression
apply_for_each(alist, alambda):foreach i in alist:
alambda( i )
apply_for_each([1,2,3], square)apply_for_each([ ... ], anything_you_want_applied_to)
apply_for_each => map/each
General LISt Processing
Map:[x,y,z] -> map(op,[x,y,z])
=> [op(x),op(y),op(z)]Reduce:
[x,y,z] -> reduce(op,[x,y,z],v) => op(op(op(v,x),y),z)
Filter:[x,y,z] -> filter(op,[x,y,z])
=> a subset of the list for each op(i) => true
more..
each, find, every/all, any/some, invoke, where, sort, group/groupBy, countBy
.... (underscore.js/Ruby Enumerable Module)
var all_of_usdef lonelyboydef lonelygirlzip( filter( lonelyboy, all_of_us), filter( lonelygirl, all_of_us) )
Lambda Calculus
lambda x: x
lambda x,y: y(x)
(lambda x:x)(1)
1
xx.λxyyx ..λλ
1).( xxλxfxf ..λλ
Recursion
Y := λg.(λx.g (x x)) (λx.g (x x))Y f=> λg.(λx.g (x x)) (λx.g (x x)) f=> (λx.f (x x)) (λx.f (x x))=> f ((λx.f (x x)) (λx.f (x x)))=> f (Y f)
SKI Combinator
I := λx.xK := λx.λy.xS := λx.λy.λz.x z (y z)
OR:
Ix = xKxy = xSxyz = xz(yz)
self-application
SIIα = Iα(Iα) = αα
SII(SII)= I(SII)(I(SII))= I(SII)(SII)= SII(SII)
reverse
S(K(SI))K αβ →K(SI)α(Kα) β →SI(Kα) β →Iβ(Kαβ) →Iβαβα
Y in SKI
Y = S (K (S I I)) (S (S (K S) K) (K (S I I)))
Y' = S S K (S (K (S S (S (S S K)))) K)