结合Lambda的阶乘累加方法
/// <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
补充:软件开发 , C# ,