2

我一直在研究 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有点像函数的参数!?

4

1 回答 1

2

y 有点像函数的参数!?

对,就是这样。Lambda 演算是隐式柯里化的。而不是x x你还不如写\y.x x y

于 2014-09-12T09:25:56.853 回答