Skip to content

Instantly share code, notes, and snippets.

@nelanka
Created July 31, 2015 14:44
Show Gist options
  • Save nelanka/37ac355c796c79ea3d7f to your computer and use it in GitHub Desktop.
Save nelanka/37ac355c796c79ea3d7f to your computer and use it in GitHub Desktop.
Tail Recursive Fibonacci
def fib(n: Int): Int = {
@tailrec
def recFib(a: Int, b: Int, i: Int): Int = i match {
case 0 => a
case _ => recFib(b, a + b, i - 1)
}
recFib(0, 1, n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment