Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -4,33 +4,32 @@ typedef int (^Func)(int);
|
|||
typedef Func (^FuncFunc)(Func);
|
||||
typedef Func (^RecursiveFunc)(id); // hide recursive typing behind dynamic typing
|
||||
|
||||
Func fix (FuncFunc f) {
|
||||
Func Y(FuncFunc f) {
|
||||
RecursiveFunc r =
|
||||
^(id y) {
|
||||
RecursiveFunc w = y; // cast value back into desired type
|
||||
return f(^(int x) {
|
||||
return w(w)(x);
|
||||
});
|
||||
};
|
||||
^(id y) {
|
||||
RecursiveFunc w = y; // cast value back into desired type
|
||||
return f(^(int x) {
|
||||
return w(w)(x);
|
||||
});
|
||||
};
|
||||
return r(r);
|
||||
}
|
||||
|
||||
int main (int argc, const char *argv[]) {
|
||||
@autoreleasepool {
|
||||
|
||||
FuncFunc almost_fac = ^Func(Func f) {
|
||||
return ^(int n) {
|
||||
if (n <= 1) return 1;
|
||||
return n * f(n - 1);
|
||||
};
|
||||
};
|
||||
|
||||
FuncFunc almost_fib = ^Func(Func f) {
|
||||
Func fib = Y(^Func(Func f) {
|
||||
return ^(int n) {
|
||||
if (n <= 2) return 1;
|
||||
return f(n - 1) + f(n - 2);
|
||||
};
|
||||
};
|
||||
});
|
||||
Func fac = Y(^Func(Func f) {
|
||||
return ^(int n) {
|
||||
if (n <= 1) return 1;
|
||||
return n * f(n - 1);
|
||||
};
|
||||
});
|
||||
|
||||
Func fib = fix(almost_fib);
|
||||
Func fac = fix(almost_fac);
|
||||
5
Task/Y-combinator/Objective-C/y-combinator-2.m
Normal file
5
Task/Y-combinator/Objective-C/y-combinator-2.m
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Func Y(FuncFunc f) {
|
||||
return ^(int x) {
|
||||
return f(Y(f))(x);
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue