Y Combinator的各种实现

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 extends Function, F> { }
  public static  Function Y(Function, Function> f) {
    RecursiveFunction> r = w -> f.apply(x -> w.apply(w).apply(x));
    return r.apply(r);
  }
 
  public static void main(String... arguments) {
    Function fib = Y(f -> n ->
      (n <= 2)
        ? 1
        : (f.apply(n - 1) + f.apply(n - 2))
    );
    Function 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));
  }
}

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



你可能感兴趣的:(Java,JavaScript,Y)