Successfully reported this slideshow.
Upcoming SlideShare
×

# Deriving the Y Combinator

1,131 views

Published on

（日本語説明下記）Here I try to derive the Y Combinator as simple as possible.

こっちの Y Combinatorを検索したいときは、
「Y Combinator 再帰」「Y Combinator ラムダ」などで検索してみてください。

Published in: Engineering
• Full Name
Comment goes here.

Are you sure you want to Yes No
• WONDERFUL JOB!.... STARTUPS...Send your pitchdeck to over 5700 of VC's and Angel's with just 1 click. Visit: Angelvisioninvestors.com

Are you sure you want to  Yes  No
• BTW nice presentation!.... Also you can send your pitchdeck to thousands of VC's and Angel's with just 1 click. Visit: Angelvisioninvestors.com

Are you sure you want to  Yes  No
• great job!!!!!!!!!!!!!! you gave a nice presentation today.

Are you sure you want to  Yes  No

### Deriving the Y Combinator

1. 1. Deriving the Y Combinator Yuta Okazaki! Recurse Center Spring 2 2015
2. 2. Recursive Function factorial = function(n){ if(n == 0){ return 1 } else{ n * factorial(n - 1) } } Pseudo Code
3. 3. Recursive Function let’s simplify this to an ‘inﬁnite’ function. fact-forever = function(n){ n * fact-forever(n - 1) }
4. 4. Recursive Function What if we can’t use a variable? fact-forever = function(n){ n * fact-forever(n - 1) }
5. 5. Recursive Function What is this inside ‘fact-forever’ gonna be? fact-forever = function(n){ n * fact-forever(n - 1) }
6. 6. function(n){ ! n * (n - 1) ! } Recursive Function I’m imagining if we have a ‘magical function’, that prepares itself again when we need it…
7. 7. Thought Experiment with Scheme(Lisp)
8. 8. Thought Experiment #1 Is it possible to think of a lambda, which returns a lambda, that takes ‘itself’ as an argument? ((lambda(me)(me me)) (lambda(x)(quote hi)))
9. 9. Thought Experiment #1 Is it possible to think of a lambda, which returns a lambda, that takes ‘itself’ as an argument? ((lambda(me)(me me)) (lambda(x)(quote hi))) => ‘hi
10. 10. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #2 Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it? quote hi
11. 11. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #2 Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it? x
12. 12. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #2 Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it? => expects 1 argument, but found none. x
13. 13. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #3 Yeah my bad, x is a lambda expecting one argument. so… pass in x itself maybe? x
14. 14. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #3 Yeah my bad, x is a lambda expecting one argument. so… pass in x itself maybe? xx =>
15. 15. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #3 Yeah my bad, x is a lambda expecting one argument. so… pass in x itself maybe? xx ((lambda(x)(x x)) (lambda(x)(x x))) =>
16. 16. ((lambda(me)(me me)) (lambda(x)( ))) Thought Experiment #3 Yeah my bad, x is a lambda expecting one argument. so… pass in x itself maybe? => infinite loop! xx ((lambda(x)(x x)) (lambda(x)(x x))) =>
17. 17. Thought Experiment #3 This is the Javascript version of same thing. function(x){ return x(x) }(function(x){return x(x)})
18. 18. ((lambda(me)(me me)) (lambda(x)( ))) Resume the Problem So, how can we use this piece with the original problem? xx function(n) n * (n - 1)
19. 19. ((lambda(x) x )(quote hi)) Functional Refactoring Wrap Function ((lambda(y)((lambda(x)x)y))(quote hi)) if you have a lambda that takes one argument, you can wrap it with another lambda which also takes one argument, and call it with that argument, you still get the same result.
20. 20. ((lambda(me)( me me )) (lambda(x) Combine them So, here I use a twisted version of Wrap Function, in order to accumulate the calculation. (lambda(number)(number * ( ( x x ))) (number - 1))))
21. 21. ((lambda(me)( me me )) (lambda(x) Combine them So, here I use a twisted version of Wrap Function, in order to accumulate the calculation. (lambda(number)(number * ( ( x x ) )) (number - 1))))
22. 22. ((lambda(me)( me me )) (lambda(x) Combine them So, here I use a twisted version of Wrap Function, in order to accumulate the calculation. (lambda(number)(number * ( ( x x ) )) (number - 1))))
23. 23. ((lambda(me)( me me )) (lambda(x) Combine them So, here I use a twisted version of Wrap Function, in order to accumulate the calculation. (lambda(number)(number * ( ( x x ) )) (number - 1))))
24. 24. Ω Combinator ((lambda(me)( me me )) (lambda(x) The code below is an application of Ω Combinator. (lambda(number)(number * ( ( x x ) )) (number - 1)))) Demo Available
25. 25. Where’s Y Combinator? Y Combinator is a nicer form of Omega Combinator. If you want to derive one, please talk to me! (lambda(le) ((lambda(f)(f f)) (lambda(f) (le (lambda(x) ((f f) x)))))) Twitter @kenzan100