Skip to content

Instantly share code, notes, and snippets.

@ryohji
Created September 12, 2019 15:29
Show Gist options
  • Save ryohji/7841bc139bd977ffd4a4d68dba7f0574 to your computer and use it in GitHub Desktop.
Save ryohji/7841bc139bd977ffd4a4d68dba7f0574 to your computer and use it in GitHub Desktop.
Z combinator
#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