How to create a recursive lambda function ?
-
Hi: How to define a recursive lambda function using C#? By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles
= Odd(a, (double x) => x + 2.0); Any ideas? :^)Ieshua Carroll Systems Engineer
-
Hi: How to define a recursive lambda function using C#? By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles
= Odd(a, (double x) => x + 2.0); Any ideas? :^)Ieshua Carroll Systems Engineer
-
Try Anonymous Recursion in C# – Yet Another Language Geek[^] :)
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]
;) It is an useful link. Thanks !
-
;) It is an useful link. Thanks !
-
Hi: How to define a recursive lambda function using C#? By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles
= Odd(a, (double x) => x + 2.0); Any ideas? :^)Ieshua Carroll Systems Engineer
However one should read the following and understand it. StackOverflowException Class (System)[^] Especially understand that it will terminate the AppDomain absolutely. And read the following... "if your app depends on recursion, use a counter or a state condition to terminate the recursive loop. The following example uses a counter to ensure that the number of recursive calls to the Execute method do not exceed a maximum defined by the MAX_RECURSIVE_CALLS constant. "
-
Hi: How to define a recursive lambda function using C#? By example, I was using the following sentence to get the even numbers in a Real group:
double[] doubles
= Odd(a, (double x) => x + 2.0); Any ideas? :^)Ieshua Carroll Systems Engineer
If you're using a recent version of the C# compiler, you might want to consider local functions: Local functions (C# Programming Guide) | Microsoft Docs[^] Using the function from the blog post that Eddy linked to, it works almost without change:
int Fib(int n) => n > 1 ? Fib(n - 1) + Fib(n - 2) : n;
Console.WriteLine(Fib(6)); // displays 8(Of course, the best solution for calculating Fibonacci numbers is to avoid recursion. :) )
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer