Several days ago we've arrived to the blog "Recursive lambda expressions". There, author asks how to write a lambda expression that calculates a factorial (only expression statements are allowed).
The problem by itself is rather artificial, but at times you feel an intellectual pleasure solving such tasks by yourself. So, putting original blog post aside we devised our answers. The shortest one goes like this:
factorial(factorial, n) = n <= 1 ? 1 : n * factorial(factorial, n - 1);
delegate int Impl(Impl impl, int n);
Impl impl = (f, n) => n <= 1 ? 1 : n * f(f, n - 1); Func<int, int> factorial = i => impl(impl, i);
Func<int, int> factorial = i => ((Func<Impl, int>)(f => f(f, i)))((f, n) => n <= 1 ? 1 : n * f(f, n - 1));
var f = factorial(10);
After that excercise we've returned back to original blog and compared solutions. We can see that author appeals to a set theory but for some reason his answer is more complex than nesessary, but comments contain variants that analogous to our answer.
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u