我一直在研究 Y Combinator,我知道它是如何在纸上工作的,但我还不知道如何用编程语言来实现它。
根据此页面: http: //matt.might.net/articles/implementation-of-recursive-fixed-point-y-combinator-in-javascript-for-memoization/
Y 组合子的推导如下:
Y(F) = F(Y(F))
# Of course, if we tried to use it, it would never work because the function Y immediately calls itself, leading to infinite recursion.
# Using a little λ-calculus, however, we can wrap the call to Y in a λ-term:
Y(F) = F(λ x.(Y(F))(x))
# Using another construct called the U combinator, we can eliminate the recursive call inside the Y combinator, which, with a couple more transformations gets us to:
Y = (λh.λF.F(λ x.((h(h))(F))(x))) (λh.λF.F(λ x.((h(h))(F))(x)))
他怎么能扩展Y(F)
成λ x.(Y(F))(x)
?他如何使用 U Combinator?
这是 Javascript 和 Elixir 中的实现:
# javascript
var Y = function (F) {
return (function (x) {
return F(function (y) { return (x(x))(y);});
})(function (x) {
return F(function (y) { return (x(x))(y);});
});
};
# elixir
defmodule Combinator do
def fix(f) do
(fn x ->
f.(fn y -> (x.(x)).(y) end)
end).(fn x ->
f.(fn y -> (x.(x)).(y) end)
end)
end
end
如果这是公式:Y = \f.(\x.f(x x))(\x.f(x x))
,那么 lambda 表达式中的 f, x 与上述实现中的 f, x, y 之间的关系是什么?x 看起来是同一个 x,f 看起来是同一个 f。那是什么y
?具体来说,为什么 lambda 等效于x x
被包装在使用的函数中y
?
y
有点像函数的参数!?