Search code examples
c#haskellfunctional-programmingfixpoint-combinators

Fixed Point Generator in C# Generics


I have tried to define a fixed point generator in C# that you see in many functional languages. I believe foldr is usually defined in terms of a fixed point generator sometimes. I'll show it's Haskell definition and then what I have in C#. Any help is greatly appreciated.

//Haskell
fix f = f (fix f)

//C# (Many attempts)
public static Func<Func<T, T>, T> Combinator1<T>(this Func<T, T> f)
{
    return x => f(Combinator1(f)(x));
}
public static Func<Func<T, T>, T> Combinator2<T>(this Func<T, T> f)
{
    return x => x(Combinator2(x)(f));
}
public static Func<T, U> Combinator3<T, U>(Func<Func<T, U>, Func<T, U>> f)
{
    return f(x => Combinator3(f)(x));
}

Solution

  • I don't have much understanding of haskell or this operator. But I have read an article from Mads Torgersen about implementing the Y/Fix combinator using C# lambda expressions. It may be of some use to you, here is the link.

    And here is the final method he implements:

    public Func<T, T> Fix<T>(Func<Func<T,T>, Func<T,T>> F) {
      return t => F(Fix(F))(t);
    }