Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,23 +1,3 @@
|
|||
import java.util.function.Function;
|
||||
|
||||
public class YCombinator {
|
||||
interface RecursiveFunc<F> extends Function<RecursiveFunc<F>, F> { }
|
||||
public static <A,B> Function<A,B> fix(Function<Function<A,B>, Function<A,B>> f) {
|
||||
RecursiveFunc<Function<A,B>> r = w -> f.apply(x -> w.apply(w).apply(x));
|
||||
return r.apply(r);
|
||||
public static <A,B> Function<A,B> Y(Function<Function<A,B>, Function<A,B>> f) {
|
||||
return x -> f.apply(Y(f)).apply(x);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
Function<Integer,Integer> fib = fix(f -> n -> {
|
||||
if (n <= 2) return 1;
|
||||
return f.apply(n - 1) + f.apply(n - 2);
|
||||
});
|
||||
Function<Integer,Integer> fac = fix(f -> n -> {
|
||||
if (n <= 1) return 1;
|
||||
return n * f.apply(n - 1);
|
||||
});
|
||||
|
||||
System.out.println("fib(10) = " + fib.apply(10));
|
||||
System.out.println("fac(10) = " + fac.apply(10));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue