(日本語説明下記)Here I try to derive the Y Combinator as simple as possible.
Yコンビネータは、変数を使わずに再帰関数をつくるための便利な考え方です。 Y Combinatorという名前は、同名のシリコンバレーの起業家支援プログラムが登場したため、一躍有名になりました(Google検索が難しくなりました)。
こっちの Y Combinatorを検索したいときは、
「Y Combinator 再帰」「Y Combinator ラムダ」などで検索してみてください。
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. 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
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
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. ((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. Thought Experiment #3
This is the Javascript version of same thing.
function(x){
return x(x)
}(function(x){return x(x)})
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. ((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. ((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. ((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. ((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. Ω 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. 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