public void Returns_final_result_after_recursion() { RecursionResult <int> Factorial(int k) { RecursionResult <int> FactorialAcc(int m, int acc) { if (m == 1) { return(RecursionResult <int> .Final(acc)); } return(RecursionResult <int> .Next(() => FactorialAcc(m - 1, m *acc))); } return(FactorialAcc(k, 1)); } Assert.Equal(5 * 4 * 3 * 2 * 1, TailRecursion.Run(() => Factorial(5))); }
public void Deep_recursion_does_not_blow_the_stack() { RecursionResult <BigInteger> Factorial(int k) { RecursionResult <BigInteger> FactorialAcc(int m, BigInteger acc) { if (m == 1) { return(RecursionResult <BigInteger> .Final(acc)); } return(RecursionResult <BigInteger> .Next(() => FactorialAcc(m - 1, m *acc))); } return(FactorialAcc(k, 1)); } var ex = Record.Exception(() => TailRecursion.Run(() => Factorial(20000))); Assert.Null(ex); }
public void Returns_final_result_from_next_iteration() { RecursionResult <int> F() => RecursionResult <int> .Next(() => RecursionResult <int> .Final(2)); Assert.Equal(2, TailRecursion.Run(F)); }