Created
August 7, 2017 17:37
-
-
Save coquin/f7b04c9a1ec81d9aae512adbb0f21028 to your computer and use it in GitHub Desktop.
Fibonacci numbers with tail recursion in Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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