Recursive lambda | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Recursive lambda

Hi I read somewhere that recursive lambda is possible but when I try to implement, I am not getting how to solve error compilation provides... Below is code : https://code.sololearn.com/c3PQoq9y6LxT/?ref=app Also I don't understand need of recursive lambda as lambda is useful when we don't want to create function and simply call it while defining the same... correct?

17th Nov 2020, 8:45 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 5
CarrieForle your approach works only because the function object is a global variable, which is accesible inside a lambda. If instead one wants to define a lamda inside a function (which is the common use case) , local variables have to be either explicitly captured, or passed as parameters like Ketan Lalcheta does. Also saving to std::function is not really needed here. What is important is to define the return type. Edit: sorry my previous statement about c++14 allowing the code without return type was wrong - a type declaration is still needed, or the code has to be rewritten to have a return before calling recursive function (like attached) https://code.sololearn.com/c4l4kBgQNDke/?ref=app
17th Nov 2020, 12:32 PM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 5
A very nice way to make a recursive lambda indeed Volodymyr Chelnokov. I also wanna tell you that the method I provide can also be used in main() and any other function. Also you mentioned the issue is auto deduction. That's why std::function is used in my method so it can fully specify the type of lambda rather than auto. But your method is better than mine since you don't need additional std::function for this.
17th Nov 2020, 2:15 PM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
First, why do you need display parameter which you never use it. You don't need a parameter of itself for recursive function. You have to specify the function return type in lambda recursive because when calling the lambda in the lambda, the lambda inside of it have no idea what its type is since the lambda itself hasn't end and can't be deduced. You will need a function object from <functional>
17th Nov 2020, 9:56 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 4
For some reason (maybe someone more versed in c++ can explain) the compiler fails to notice that your function never returns anything, so its return type should be void, despite the fact that we don't really know the return type of the parameter. So as CarrieForle told, you just need to specify the return type like this: constexpr auto display = [] (const auto display, int intCnt) -> void {...} ; Having a recursive lambda can be useful for example when parametrizing the graph algorithms. Unfortunately the way you can do it now makes them less useful and less clear to use -- you need to give a name to your lambda. In most cases you are better off just creating a callable class (like people did before lambdas were introduced)
17th Nov 2020, 11:42 AM
Volodymyr Chelnokov
Volodymyr Chelnokov - avatar
+ 4
Instead of auto, use function<void(int)> where function is from <fucntional> And remove the first parameter because you never use it.
17th Nov 2020, 11:45 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
Thanks for helping me understand Volodymyr Chelnokov and CarrieForle ... appreciate your help
18th Nov 2020, 12:39 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
I am sorry but not getting what you mean to say by specify the function return type in lambda recursive... Also what is related to functional header ? Could you please elaborate ?
17th Nov 2020, 11:34 AM
Ketan Lalcheta
Ketan Lalcheta - avatar