Skip to content

Instantly share code, notes, and snippets.

@ehsanullahjan
Last active August 29, 2015 14:23
Show Gist options
  • Save ehsanullahjan/3eaf20df988edea00b1c to your computer and use it in GitHub Desktop.
Save ehsanullahjan/3eaf20df988edea00b1c to your computer and use it in GitHub Desktop.
Fibonacci using tail recursion
def fib(n: Long): Long = {
@annotation.tailrec
def go(n: Long, x: Long, y: Long): Long =
n match {
case 0 => x
case 1 => y
case _ => go(n - 1, y, x + y)
}
go(n, 0, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment