среда, 7 ноября 2007 г.

Тихо шифером шурша...вычисляем факториал на C#

using System;

 

namespace sillytests

{

    delegate T SelfApplicable<T>(SelfApplicable<T> self);

 

    class Program

    {

        static void Main(string[] args)

        {

            // The Y combinator

            SelfApplicable< Func<Func< Func<int, int>, Func<int, int>>, Func<int, int>>> Y = y => f => x => f(y(y)(f))(x);

            // The fixed point generator

 

            Func< Func< Func<int, int>, Func<int, int> >, Func<int, int>> Fix = Y(Y);

            // The higher order function describing factorial

            Func<Func<int, int>, Func<int, int>> F = fac => x => x == 0 ? 1 : x * fac(x - 1);

 

            // The factorial function itself

            Func<int, int> factorial = Fix(F);

 

            for (int i = 0; i < 12; i++)

            {

                Console.WriteLine(factorial(i));

            }

        }

 

    }

}



Взято тут.

Комментариев нет: