I was playing with recursive lambdas in C# and have found two approaches to do this on the web. One approach uses fixed point combinator and the other does not. In the code below f1 is built using combinator, and f2 is defined directly. My question is, do we need fixed point combinators in C# or the language already provides all we need, so we can leave them alone?
class Program
{
static Func<T, T> F<T>(Func<Func<T,T>,Func<T,T>> f)
{
return x => f(F(f))(x);
}
static void Main(string[] args)
{
Func<Func<int,int>,Func<int,int>> f = fac => x => x == 0 ? 1 : x * fac(x - 1);
var f1 = F(f);
Console.WriteLine(f1(5));
Func<int, int> f2 = null;
f2 = x => x == 0 ? 1 : x * f2(x - 1);
Console.WriteLine(f2(5));
}
}