Recursive Lambda Expressions in C#

The first two samples, loop & loop2, show you a recursive Lambda Expression. The first version has some console outputs for debugging. I’ve also added a sample that gives you the 10th Fibonacci number.

The other samples are just showing you some syntactic sugar that can be used when working with Lambda Expressions.

using System;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Func<int, string> consoleResult = x => string.Format("Result: {0}" + Environment.NewLine, x);

// loop version 1 (debug)
Func<int, int> loop = null;
loop = x => { Console.WriteLine(x); return x == 0 ? 1 : x * loop(x - 1); };
Console.WriteLine(consoleResult(loop(5)));

// loop version 2
Func<int, int> loop2 = null;
loop2 = x => x == 0 ? 1 : x * loop(x - 1);
Console.WriteLine(consoleResult(loop2(5)));


// Fibonnaci
Func<int, int> fib = null;
fib = x => x < 2 ? x : fib(x - 1) + fib(x - 2);
Console.WriteLine(consoleResult(fib(10)));

//-----------------------//
// Some other examples
//-----------------------//

// version 1
Func<int, int> calc = null;
calc = (x) => { Console.WriteLine(x); return x >= 1024 ? x : calc(x * 2); };
Console.WriteLine(consoleResult(calc(2)));

// version  2
Func<int, int> calc2 = null;
calc2 = x => x >= 1024 ? x : calc2(x * 2);
Console.WriteLine(consoleResult(calc2(2)));

// version 3
Func<int, int> calc3 = null;
calc3 = x => x >= 1024 ? x : calc3(x * 2);
Console.WriteLine(consoleResult(calc3(2)));

// version 4
Func<int, int> calc4 = null;
calc4 = x => { return x >= 1024 ? x : calc4(x * 2); };
Console.WriteLine(consoleResult(calc4(2)));

// version 5
Func<int, int> calc5 = null;
calc5 = (x) => x >= 1024 ? x : calc5(x * 2);
Console.WriteLine(consoleResult(calc5(2)));

// version 6
Func<int, int> calc6 = null;
calc6 = (x) => { return x >= 1024 ? x : calc6(x * 2); };
Console.WriteLine(consoleResult(calc6(2)));

// Don't close the console directly
Console.ReadLine();
}
}
}

You will see a compiler error Use of unassigned local variable ‘fib’  when you try something like this:

Func<int, int> fib = x => x < 2 ? x : fib(x - 1) + fib(x - 2);

The compiler cannot use fib because it is not yet assigned.
(Depends on which side the C# Compiler evaluates first).

Have fun exploring Lambda Expressions, recursion and Func<T, TResult> 😉

Advertisements

1 thought on “Recursive Lambda Expressions in C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s