Y Combinator的各种实现

1 篇文章 0 订阅
1 篇文章 0 订阅

1.JavaScript:

Rossetta Code:

function Y(f) {
    var g = f((function(h) {
        return function() {
            var g = f(h(h));
            return g.apply(this, arguments);
        }
    })(function(h) {
        return function() {
            var g = f(h(h));
            return g.apply(this, arguments);
        }
    }));
    return g;
}
 
var fac = Y(function(f) {
    return function (n) {
        return n > 1 ? n * f(n - 1) : 1;
    };
});
 
var fib = Y(function(f) {
    return function(n) {
        return n > 1 ? f(n - 1) + f(n - 2) : n;
    };
});


Douglas Crockford:

function Y(le) {
    return (function (f) {
        return f(f);
    }(function (f) {
        return le(function (x) {
            return f(f)(x);
        });
    }));
}
var factorial = Y(function (fac) {
    return function (n) {
        return n <= 2 ? n : n * fac(n - 1);
    };
});

var number120 = factorial(5);



  
  
推导过程版本,from : g9yuayon


2.Java:

version 8+:

import java.util.function.Function;
 
public interface YCombinator {
  interface RecursiveFunction<F> extends Function<RecursiveFunction<F>, F> { }
  public static <A,B> Function<A,B> Y(Function<Function<A,B>, Function<A,B>> f) {
    RecursiveFunction<Function<A,B>> r = w -> f.apply(x -> w.apply(w).apply(x));
    return r.apply(r);
  }
 
  public static void main(String... arguments) {
    Function<Integer,Integer> fib = Y(f -> n ->
      (n <= 2)
        ? 1
        : (f.apply(n - 1) + f.apply(n - 2))
    );
    Function<Integer,Integer> fac = Y(f -> n ->
      (n <= 1)
        ? 1
        : (n * f.apply(n - 1));
    );
 
    System.out.println("fib(10) = " + fib.apply(10));
    System.out.println("fac(10) = " + fac.apply(10));
  }
}
<java 8:

interface Function<A, B> {
    public B call(A x);
}
 
public class YCombinator {
    interface RecursiveFunc<F> extends Function<RecursiveFunc<F>, F> { }
 
    public static <A,B> Function<A,B> fix(final Function<Function<A,B>, Function<A,B>> f) {
        RecursiveFunc<Function<A,B>> r =
            new RecursiveFunc<Function<A,B>>() {
            public Function<A,B> call(final RecursiveFunc<Function<A,B>> w) {
                return f.call(new Function<A,B>() {
                        public B call(A x) {
                            return w.call(w).call(x);
                        }
                    });
            }
        };
        return r.call(r);
    }
 
    public static void main(String[] args) {
        Function<Function<Integer,Integer>, Function<Integer,Integer>> almost_fib =
            new Function<Function<Integer,Integer>, Function<Integer,Integer>>() {
            public Function<Integer,Integer> call(final Function<Integer,Integer> f) {
                return new Function<Integer,Integer>() {
                    public Integer call(Integer n) {
                        if (n <= 2) return 1;
                        return f.call(n - 1) + f.call(n - 2);
                    }
                };
            }
        };
 
        Function<Function<Integer,Integer>, Function<Integer,Integer>> almost_fac =
            new Function<Function<Integer,Integer>, Function<Integer,Integer>>() {
            public Function<Integer,Integer> call(final Function<Integer,Integer> f) {
                return new Function<Integer,Integer>() {
                    public Integer call(Integer n) {
                        if (n <= 1) return 1;
                        return n * f.call(n - 1);
                    }
                };
            }
        };
 
        Function<Integer,Integer> fib = fix(almost_fib);
        Function<Integer,Integer> fac = fix(almost_fac);
 
        System.out.println("fib(10) = " + fib.call(10));
        System.out.println("fac(10) = " + fac.call(10));
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值