Of course, in C ++, to call any function, you have to bind it to an identifier somewhere, simply because of syntactic restrictions. But, if you accept the parameters as not sufficiently named, then you can create a version of y-combinator in C ++ that will look good without being βnamedβ.
Now this is really ugly because I don't know how to make a typedef for a recursive lambda . So he just uses a lot of abuse. But it works and prints FLY!! until segfault happens due to.
#include <iostream> typedef void(*f0)(); typedef void(*f)(f0); int main() { [](fx) { x((f0)x); } ([](f0 x) { std::cout<<"FLY!!\n"; ((f)x)(x); }); }
The two lambdas are unnamed in the sense that none of them are assigned anywhere. The second lambda is a real workhorse, and it basically calls itself using the first lambda to get a link to itself as a parameter.
Here's how you could use this to do a βusefulβ job:
#include <iostream> typedef int param_t; typedef int ret_t; typedef void(*f0)(); typedef ret_t(*f)(f0, param_t); int main() { /* Compute factorial recursively */ std::cout << [](fx, param_t y) { return x((f0)x, y); } ([](f0 x, param_t y) { if(y == 0) return 1; return y*((f)x)(x, y-1); }, 10) << std::endl; }
nneonneo
source share