Introduction to lambda expression & lambda calculus

15
Requirements Common Knowledge of Programming Maybe Python/Ruby Recursion

Transcript of Introduction to lambda expression & lambda calculus

Page 1: Introduction to lambda expression & lambda calculus

Requirements

• Common Knowledge of Programming• Maybe Python/Ruby• Recursion

Page 2: Introduction to lambda expression & lambda calculus

LAMBDA INTRO

KIM LEO

Page 3: Introduction to lambda expression & lambda calculus

λ

Page 4: Introduction to lambda expression & lambda calculus

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?

Page 5: Introduction to lambda expression & lambda calculus

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 )

Page 6: Introduction to lambda expression & lambda calculus

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 )

.....

Page 7: Introduction to lambda expression & lambda calculus

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

Page 8: Introduction to lambda expression & lambda calculus

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

Page 9: Introduction to lambda expression & lambda calculus

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) )

Page 10: Introduction to lambda expression & lambda calculus

Lambda Calculus

lambda x: x

lambda x,y: y(x)

(lambda x:x)(1)

1

xx.λxyyx ..λλ

1).( xxλxfxf ..λλ

Page 11: Introduction to lambda expression & lambda calculus

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)

Page 12: Introduction to lambda expression & lambda calculus

SKI Combinator

I := λx.xK := λx.λy.xS := λx.λy.λz.x z (y z)

OR:

Ix = xKxy = xSxyz = xz(yz)

Page 13: Introduction to lambda expression & lambda calculus

self-application

SIIα = Iα(Iα) = αα

SII(SII)= I(SII)(I(SII))= I(SII)(SII)= SII(SII)

Page 14: Introduction to lambda expression & lambda calculus

reverse

S(K(SI))K αβ →K(SI)α(Kα) β →SI(Kα) β →Iβ(Kαβ) →Iβαβα

Page 15: Introduction to lambda expression & lambda calculus

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)