0

I am trying to write Y combinator as C# delegate so I can understand the types but I am failing. I appreciate any help or hint.

Rec<T> Y<T>(ToRec<T> f)
{
    Rec<T> nested(Rec<T> x)
    {
        return f(x(x));
    }
    
    return nested(nested);
}

Y<string>(Y);  // This line doesn't type check

delegate Rec<T> ToRec<T>(T x);
delegate T Rec<T>(Rec<T> x);
Node.JS
  • 1,042
  • 6
  • 44
  • 114
  • `nested(nested)` seems to give an error aswell - https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRjz1QIE4AKAIkB4NwEH2qBKAbmNzwCUBTAYwB4AKgD4ABAE1BQigID23fsJEAzRngDeeEVpHzJIqFwDOwLnAq7FAD1W5tIjbbvbUAdmUVLHxi03aAvr5agSKu+kYmZgbGpj64Aey4APSJEqSYUmIsWskiAgAWAJaGIgA2BQYicDJGUADkwCLAAJ4ADlwiPHm8ANZscFwlXADmAIYmOrx6shZSAiLWrLj9g6PjczPmk1YsQA== – Rand Random Dec 28 '22 at 07:18
  • How about post full code – MichaelMao Dec 28 '22 at 07:36
  • Your `Y` method doesn't except a delegate of type `Y` so that will be a type error. If you want to return a local function, you need to use `Func` or define a delegate with the correct return type. What's wrong with the implementation here: https://stackoverflow.com/a/31821236/1462295 ? – BurnsBA Dec 28 '22 at 16:26

0 Answers0