# Y Combinator

``````def fact(n):
return 1 if(n < 2) else n * fact(n - 1)``````

``lambda fact: lambda n: 1 if n < 2 else n * fact(n - 1)``

``````def y(f):
# 假設存在一個 fact
return f(fact)``````

``````def y(f):
fact = lambda n: f(fact)(n)
return f(fact)``````

``````def y(f):
def get_f():
# 假設都用匿名函式實現 ...
return get_f()``````

``````def y(f):
def get_f():
fact = lambda n: f(fact)(n)
return f(fact)
return get_f()``````

``````def y(f):
def get_f():
fact = lambda n: get_f()(n)
return f(fact)
return get_f()``````

``````def y(f):
def get_f():
return f(lambda n: get_f()(n))
return get_f()``````

``````...
def get_f(get_f):
return f(lambda n: get_f(get_f)(n))
...``````

``````def y(f):
def get_f(get_f):
return f(lambda n: get_f(get_f)(n))
return get_f(get_f)``````

``````def y(f):
get_f = lambda get_f: f(lambda n: get_f(get_f)(n))
return get_f(get_f)``````

``````def y(f):
get_f = lambda get_f: f(lambda n: get_f(get_f)(n))
return (lambda get_f: f(lambda n: get_f(get_f)(n)))(lambda get_f: f(lambda n: get_f(get_f)(n)))``````

``````def y(f):
return (lambda get_f: f(lambda n: get_f(get_f)(n)))(lambda get_f: f(lambda n: get_f(get_f)(n)))``````

``y = lambda f: (lambda x: f(lambda n: x(x)(n)))(lambda x: f(lambda n: x(x)(n)))``

``y(lambda fact: lambda n: 1 if n < 2 else n * fact(n - 1))(5) # 120``

``y(lambda fib: lambda n: n if(n == 0 or n == 1) else fib(n - 1) + fib(n - 2))(10) # 55``