Created
September 12, 2019 15:29
-
-
Save ryohji/7841bc139bd977ffd4a4d68dba7f0574 to your computer and use it in GitHub Desktop.
Z combinator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
int main() { | |
auto z = [](auto f) { | |
auto g = [&f](auto x) { | |
return f([&x](auto y) { return x(x)(y); }); | |
}; | |
return g(g); | |
}; | |
auto f = [](auto f) { | |
return [&f](auto x) { return x ? x * f(x-1) : 1; }; | |
}; | |
/* can't be compiled. | |
z.C:6:38: error: function 'operator()<(lambda at z.C:5:14)>' with deduced return type cannot be used before it is defined | |
return f([&x](auto y) { return x(x)(y); }); | |
*/ | |
std::printf("%d\n", z(f)(5)); // expected to print 5! | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment