/// <summary>
/// 階乘、累加
/// Func<int, int>中前一個是委托方法參數類型,後一個是委托的返回類型
/// </summary>
public class Factorial
{
/// <summary>
/// 階乘方法委托調用
/// </summary>
public static Func<int, int> functirial = RFunc<int, int>((m,n)=>n==1?1:n*m(n-1));
public static Func<int, int> cumulative = RFunc<int, int>((m, n) => n == 0 ? 0 : n +m (n- 1));
/// <summary>
/// 單參數有返回值遞歸方法生成器。
/// </summary>
/// <typeparam name="T">參數類型。</typeparam>
/// <typeparam name="TResult">返回值類型。</typeparam>
/// <param name="f">遞歸運算實現</param>
/// <returns></returns>
public static Func<T, TResult> RFunc<T, TResult>(Func<Func<T, TResult>, T, TResult> f)
{
return x => f(RFunc(f), x);
}
}
外部調用
Factorial.functirial(4)
摘自 流雲521