SIGN UP MEMBER LOGIN:    
ARTICLE

Functional Programming with C#: Dynamic List Generation

Posted by Matthew Cochran Articles | Articles C# January 27, 2008
This article covers how to use functions as the basis for generating lists given some initial seed values. This technique is useful for constructing different types of numeric series, calculating growth and decay, and also useful for searching algorithms.
Reader Level:

Part I.  A Simple Expansion

If we define a Expand() extension method for a Func<T,T> delegate type we can use it to generate lists.  This method will calculate indefinitely in the while loop so we'll have to be careful to constrain the output.

/// <summary>
///    <para>Expands indefinately starting with the seed.</para>
///    <para>Warning: this is an infinite loop and a slice of the output must be selected</para>
/// </summary>
public static IEnumerable<T> Expand<T>(this Func<T, T> generator, T pSeedA)
{
    while (true)
    {
        yield return pSeedA;
        pSeedA = generator(pSeedA);
    }
}

For an example, if we define a delegate to calculate a single decay calculation of .99 (or 1% decay), then we can  call Expand() method passing an initial seed value and our method will calculate a never-ending list of exponentially decaying results.  We'll take the 251st item to see what our value has decayed to.

Console.WriteLine("\n\nAfter 250 intervals of decay at a rate of 0.99, the value 100000.00 is now " +
    new Func<Double, Double>(x => x * 0.99)
        .Expand(100000.00) // seed value
        .Skip(250) // skip the first 250
        .First() // get the next one
); // END Console.WriteLine()

 Similarly, we can calculate exponential growth in the same manner:

Console.WriteLine("\n\nAfter a year of a monthly compounded interest rate of .08%,  10.00 grows to " +
    new Func<Double, Double>(x => x * 1.0008)
        .Expand(10.00)
        .Skip(12)
        .First()
); // END Console.WriteLine()

Part II. An Expansion with Two Seeds

We can also define an expansion used to generate a list using two seed values where each result is combined with the previous in a recursive manner.

/// <summary>
///    <para>Expands indefinately starting with the first two seeds.</para>
///    <para>Warning: this is an infinite loop and a slice must be selected</para>
/// </summary>
public static IEnumerable<T> Expand<T>(this Func<T, T, T> generator, T pSeedA, T pSeedB)
{
    T temp;

    while (true)
    {
        yield return pSeedA;
        temp = pSeedB;
        pSeedB = generator(pSeedA, pSeedB);
        pSeedA = temp;
    }
}

Using this technique, we can easily calculate a Fibonacci sequence

// Fibonacci Sequence
new Func<Int32, Int32, Int32>((x, y) => x + y)
    .Expand(1, 2)
    .Take(40)
    .ForEach(i => Console.WriteLine(i));

Part III. Searching

We can also use our simple expansion technique for searching where we need the answer to converge to an acceptable range like in this algorithm used to calculate a root of a given number. 

/// <summary>
/// Calculates the root using the really cool "Nth roots" algorithm from
/// http://www.gizmology.net/roots.htm.
/// </summary>
private static Double CalculateRoot(Double value, Double root)
{
    return new Func<Double, Double>(guess => (guess * (root - 1) + value / Math.Pow(guess, root - 1)) / root)
        .Expand(value / root) // this is our initial guess to send to the function...
        .TakeWhile((x, y) => x != y) // if x != y then we still don't have convergence
        .Last(); // once we have converged to an answer, get the last item (the answer)
}

This "converging" is a common technique in a number of AI algorithms (such as clustering and annealing algorithms) and they could be implemented as in the code above.

The code attached to this article has definitions for the Convert(), ForEach() and TakeWhile() extension methods used in the samples above.

Until next time,
Happy coding

Login to add your contents and source code to this article
share this article :
post comment
 

I like the way you used Functional Programming with the Nth root convergence algorithm. As you said, this might be cool to try with an AI algorithm such as Population based Incremental Learning. (PBIL)

Posted by Mike Gold Jan 28, 2008
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor