32. Use Tail Recursion
Question: Define the factorial function fact(n) , given n >= 0
import scala.annotation.tailrec
def fact(n: Int): Int = {
// @tailrec does not gurantee tail recursion
// it simply raise an exception when there is not
@tailrec
def factHelper(n: Int, acc: Int): Int =
if (n == 0) acc
else factHelper(n-1, acc * n)
factHelper(n, 1)
}