using System.Collections.Generic;
using System.Diagnostics;
public static void Main()
Func<uint, uint> fib = null;
fib = x => x > 1 ? fib(x - 1) + fib(x - 2) : x;
Stopwatch s = new Stopwatch();
Console.WriteLine(fib(37));
Console.WriteLine("Without optimization: {0}", s.Elapsed);
Console.WriteLine(fib(37));
Console.WriteLine("Without optimization: {0}", s.Elapsed);
public static class FuncExtensions
public static Func<A, R> Memoize<A, R>(this Func<A, R> f)
var d = new Dictionary<A, R>();
if (!d.TryGetValue(a, out r))