【问题标题】:Y-combinator implementation in javascript and elixirjavascript 和 elixir 中的 Y-combinator 实现
【发布时间】:2014-09-12 08:24:42
【问题描述】:

我一直在研究 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是什么?具体来说,为什么 x x 的 lambda 等效项被包装在使用 y 的函数中?

y 有点像函数的参数吗!?

【问题讨论】:

  • 您应该观看 Jim Weirich 的 Ruby 函数式编程视频。在演讲中,他一步步推导出 Y Combinator。非常令人印象深刻,教育和有趣的观看! youtube.com/watch?v=FITJMJjASUs

标签: javascript elixir y-combinator


【解决方案1】:

y 有点像函数的参数吗!?

是的,没错。 Lambda 演算隐含为curried。与其写x x,不如写成\y.x x y

【讨论】:

  • 是否存在被“Y 组合”的函数没有任何参数的情况,因此不需要我们包装x x?或者是否所有正在“Y 组合”的函数都有参数,因为它们是可重入的递归函数。另外,如果您要给出语义标签,您会怎么称呼“x”?
  • 更重要的是,如果总是有一个柯里化参数,为什么 lambda 演算表达式不能在Y = \f.(\x.f(x x))(\x.f(x x)) 中表示它?如果有一些东西表明x x 需要参数,那不是更清楚吗?
  • 不,Y 组合子被明确定义为单参数函数的固定点(其他一切都没有多大意义)。不知道x函数有没有名字。
  • 如果……的话,lambda 演算会更清楚吗 - 是的,也许吧。但清晰不一定是这个符号的目的 :-) 事实上,omitting parameters 通常更清晰 - 当它从类型中“明显”时,你就不需要它们
猜你喜欢
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
相关资源
最近更新 更多