by amelius · 12y ago · 0 comments
I recently tried to make a lambda function in C++ call itself. My first approach didn't work, because the variables were captured by reference, causing my program to crash (the enclosing scope had disappeared when my recursive lambda function was called). I easily tracked down that problem. But this generated a second problem: without using "capturing by reference", I couldn't make the lambda function refer to itself!
Of course, I looked around on the internet, and I found [1], which actually shows the insane amount of hacking required to make a lambda function call itself. And in the end, this solution didn't even work for me.
So, concluding, I think we're a long way from "functional programming in C++".
[1] http://cpptruths.blogspot.nl/2013/10/creating-recursive-lamb...
They do have a name, but unfortunately it's just a compiler-generated one that's not known beforehand. And you would think that because the body of the lambda is really inside an operator(), 'this' could naturally be used to refer to the lambda itself, but alas, it doesn't work:
http://stackoverflow.com/questions/14531993/can-lambda-funct...
This is one thing about lambdas that I think the standards folks messed up. Having a lambda call itself might not be such a compelling use case, but it comes from the more general fact that a lambda cannot refer to itself, even though there isn't any reason preventing it from doing so; e.g. being able to pass itself ('this') as a parameter to some other function can be very useful.
In any case, this is cheating. What you effectively have here is a pair of mutually recursive functions, Recursive<>::op() calls anonymous_lambda_type::op(), which calls Recursive<>::op() again, and so on. I checked Wikipedia without even knowing 'mutual recursion' was an official mathy thing, it just sounded right, and I'm pretty sure this label is sufficient to describe the idea.
If only even more advanced functional concepts were demonstrated without all the unnecessary pomp and ceremony common to Haskell and other functional advocates then I'm sure we'd all benefit. I'm constantly reminded when I see examples like this of Bodil Stokkes presentation 'What Every Hipster Should Know About Functional Programming'
Because that's what it is. I'm trying to communicate to people who know basic ideas from computer science. This is the last place I would expect someone to call the Y combinator too highbrow.
> In any case, this is cheating.
You're not making any sense.
It's not highbrow at all, that's what annoys me. I can describe in detail how code like this will translate to machine code, the operations on the stack, the flow control going on etc and think of several ways such constructs could be used to solve real problems. I can, however, read the entire Wikipedia page on 'Fixed-point combinators' and learn nothing beyond the first paragraph which describes their broad classification and properties.
Don't you consider it a had sign that the Wiki page for FPCs eventually reads "The analog for mutual recursion is a polyvariadic fix-point combinator, which may be denoted Y*." ... and when you Google 'polyvariadic' the first page of hits is almost entirely Haskell? ... oh and only ~5,000 results. Basic terminology indeed, clearly not niche gouging wankery.
In any case, neat solution to recursing in to a C++ lambda. I'm just venting generally.
On a site named after fixed-point recursion, no less.
A more important point is that "fixed-point combinator" tells you what the function does - it takes a function (a combinator is a function that operates on a function) and returns its fixed point; a "pair of mutually recursive functions" could do anything, and so conveys less information.
I doesn't require any hack at all, it doesn't even require to capture the variable. You just need to be sure to make it persist and be accessible in the scope of the lambda.
For example:
That said, please don't do it in production code. std::function is very inefficient when it is being used in a recursive way
Its parent scope is not persistent.
The subject was discussed in the following threads:
http://stackoverflow.com/questions/2067988/recursive-lambda-...
http://stackoverflow.com/questions/17066667/overhead-of-recu...
http://pfultz2.github.io/Fit/doc/html/fix/
So you could implement a generic factorial like this:
auto factorial = fit::fix([](auto self, auto x) -> decltype(x)
{
return x == 0 ? 1 : x * self(x-1);
});From http://www.ioccc.org/2014/whowon.html
Most functional
Freek Wiedijk - Y combinator
We'll have to patiently await publication of the source though...In any case, if calling a lambda function from within itself is going to take more than a single line of straightforward code, then I'm afraid the solution still doesn't cut it for practical purposes.
I'm guessing a cast. The infinitely long type is not at all a problem in languages with weak type systems like C.