Skip to content

Instantly share code, notes, and snippets.

@jimmydivvy
Created March 4, 2013 06:31
Show Gist options
  • Save jimmydivvy/5080391 to your computer and use it in GitHub Desktop.
Save jimmydivvy/5080391 to your computer and use it in GitHub Desktop.
Recursive Fibonacci using tail recursion
def fib( n: Long ):Long = {
def fib_tailrec( n:Long, a:Long, b:Long ):Long = {
n match {
case 0 => a
case _ => fib_tailrec( n - 1, b, a + b )
}
}
fib_tailrec(n, 0, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment