KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
My brain seems to be in masochistic mode, so after being drowned in this , this and this , it wanted to mess around with some DIY in C#. I came up with the following, which I don't think is the Y-combinator, but it does seem to manage to make a non-recursive function recursive, without referring to itself: Func<Func<dynamic, dynamic>, Func<dynamic, dynamic>> Y = x => x(x); So given these: Func<dynamic, Func<dynamic, dynamic>> fact = self => n => n == 0 ? 1 : n * self(self)(n - 1); Func<dynamic, Func<dynamic, dynamic>> fib = self => n => n < 2 ? n : self(self)(n-1) + self(self)(n-2); We can generate these: Func<dynamic, dynamic> Fact = Y(fact); Func<dynamic, dynamic> Fib = Y(fib); Enumerable.Range(0, 10) .ToList() .ForEach(i => Console.WriteLine("Fact({0})={1}", i, Fact(i))); Enumerable.Range(0, 10) .ToList() .ForEach(i => Console.WriteLine("Fib({0})={1}", i, Fib(i)));
Tags (comma-separated)
Save Edits
Cancel