7 ms·
Here is a personal anecdote. I recently tried to make a lambda function in C++ call itself. My first approach didn't work, because the variables were captured
by amelius 12y ago
Here is a personal anecdote.
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-lambdas-and.html http://cpptruths.blogspot.nl/2013/10/creating-recursive-lamb...
- readerrrr 12y agoThe article talks mostly about purity, and that is easily achieved.
- tromp 12y agoYou can in fact define a fixed point combinator in good old C99, one that compiles without warning, and doesn't take all that much code. From http://www.ioccc.org/2014/whowon.html http://www.ioccc.org/2014/whowon.html Most functional Freek Wiedijk - Y combinator We'll have to patiently await publication of the source though...
- readerrrr 12y agoDo you have any code examples?
- amelius 12y agoThe Y combinator causes problems with typing (it has an infinite recursive type), so I'm curious what solution they came up with. 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.
- anaphor 12y agoThey could've done something similar to what you do when you implement a functional language in C and used a union to represent every possible return type, and included functions (closures if you will) in that union, then you solve the problem of the infinite type.
- jjaredsimpson 12y agowhy does this matter, what is the overwhelming use case for self referential anonymous functions.
- userbinator 12y agoso I'm curious what solution they came up with. I'm guessing a cast. The infinitely long type is not at all a problem in languages with weak type systems like C.
- pmelendez 12y ago>"which actually shows the insane amount of hacking required to make a lambda function call itself" 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: http://ideone.com/6hwVGy http://ideone.com/6hwVGy That said, please don't do it in production code. std::function is very inefficient when it is being used in a recursive way
- TillE 12y agoWhy is it inefficient with recursion? I know that at least non-recursive usage of std::function is generally well-optimized by most compilers.
- pmelendez 12y agoWhen you wrap a lambda into a std::function variable, it involves a call to a virtual function which add overheads (often unnoticed unless you repeat the call several times), so a regular recursive functor would be even faster. The subject was discussed in the following threads: http://stackoverflow.com/questions/2067988/recursive-lambda-functions-in-c0x/14591727#14591727 http://stackoverflow.com/questions/2067988/recursive-lambda-... http://stackoverflow.com/questions/17066667/overhead-of-recursive-lambdas/17067042#17067042 http://stackoverflow.com/questions/17066667/overhead-of-recu...
- amelius 12y agoThe lambda function was an event handler, that needed to add itself to the event loop under certain conditions (retry). Its parent scope is not persistent.
- psykotic 12y agoYou can easily write a fixed-point combinator for this: https://gist.github.com/pervognsen/eca3bc833a65195f053c https://gist.github.com/pervognsen/eca3bc833a65195f053c. The way you tie the knot in C++ type recursion is not that different from in other languages like Haskell. In Haskell you wrap it in a newtype (http://stackoverflow.com/a/5885270 http://stackoverflow.com/a/5885270). In C++ you wrap it in a struct.
- nly 12y agoWhy does a rather trivial construct like this require an arcane label like 'fixed-point combinator'? 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' http://vimeo.com/68331937 http://vimeo.com/68331937
- psykotic 12y ago> Why does a rather trivial construct like this require an arcane label like 'fixed-point combinator'? 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.
- yzzxy 12y ago> Why does a rather trivial construct like this require an arcane label like 'fixed-point combinator'? On a site named after fixed-point recursion, no less.
- nly 12y agoHaving a lambda call itself seems like self-serving functional wankery to me. Lambdas in C++ are nothing but unnamed classes with an () operator. They're blobs of captured values (or references), made immutable by default, that happen to have a function associated with them. If you want to recurse, give the damn thing a name, or rewrite the body in an iterative form.
- deleted 12y ago[deleted]
- userbinator 12y agounnamed classes with an () operator 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-functions-be-recursive 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.
- mjevans 12y agoYou're asking for a persistent first class function; write it like one.
- pfultz2 12y agoI don't know about that link, but in general you would use a fix point combinator. The Fit library has a fix point combinator here: http://pfultz2.github.io/Fit/doc/html/fix/ 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); });