几个版本的Y组合子

C#版

static Func<T, TResult> Fix<T, TResult>(Func<Func<T, TResult>, Func<T, TResult>> f)
{
    return x => f(Fix(f))(x);
}

static Func<T1, T2, TResult> Fix<T1, T2, TResult>(Func<Func<T1, T2, TResult>, Func<T1, T2, TResult>> f)
{
    return (x, y) => f(Fix(f))(x, y);
}

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);});
  }) ;
} ;

应用

// var g = λf.λn.(n <= 1 ? 1 : n * f(n - 1))
var g = function(f) {
    return function(n) {
        if(n <= 1) {
            return 1
        } else {
            return n*f(n-1);
        }
    }
}

alert(Y(g)(5))

发表评论