Skip to content

Instantly share code, notes, and snippets.

@coquin
Created August 7, 2017 17:37
Show Gist options
  • Select an option

  • Save coquin/f7b04c9a1ec81d9aae512adbb0f21028 to your computer and use it in GitHub Desktop.

Select an option

Save coquin/f7b04c9a1ec81d9aae512adbb0f21028 to your computer and use it in GitHub Desktop.
Fibonacci numbers with tail recursion in Scala
def fib(n: Int) = {
def fib_tail(n: Int, a: Int, b: Int): Int = n match {
case 0 => a
case _ => fib_tail(n - 1, a + b, a)
}
fib_tail(n, 0, 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment