3 ms·
Just out of curiosity I've made a bug-to-bug translation of original Ruby version of the Y Combinator function from the submitted link to Python language. In R
by d0mine 18y ago
Just out of curiosity I've made a bug-to-bug translation of original Ruby version of the Y Combinator function from the submitted link to Python language.
In Ruby http://is.gd/gdJ http://is.gd/gdJ :
y = proc { |generator|
proc { |x|
proc { |*args|
generator.call(x.call(x)).call(*args)
}
}.call(proc { |x|
proc { |*args|
generator.call(x.call(x)).call(*args)
}
})
}
In Python:
# Y = λf·(λx·f (x x)) (λx·f (x x))
y = lambda generator:\
(lambda x: lambda *args: generator(x(x))(*args))\
(lambda x: lambda *args: generator(x(x))(*args))
Example with factorial:
factorial_generator = lambda callback: lambda n: n*callback(n-1) if n > 1 else 1
factorial = y(factorial_generator)
n = 5
print "%d! = %d" % (n, factorial(5))