Skip to content

Instantly share code, notes, and snippets.

@loonkwil
Created May 27, 2014 19:45
Show Gist options
  • Save loonkwil/1512d4d1f0ce3f43f10e to your computer and use it in GitHub Desktop.
Save loonkwil/1512d4d1f0ce3f43f10e to your computer and use it in GitHub Desktop.
import scala.annotation.tailrec
@tailrec
def fact(n: Int): Int = {
def loop(acc: Int, n: Int): Int =
if (n == 0) acc
else loop(acc * n, n - 1)
loop(1, n)
}
def fact(n: Int): Int = {
if (n == 0) 1
else n * fact(n - 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment