Skip to content

Instantly share code, notes, and snippets.

@krishnabhargav
Created October 21, 2014 03:14
Show Gist options
  • Save krishnabhargav/6dab91b484cb5aa8fc47 to your computer and use it in GitHub Desktop.
Save krishnabhargav/6dab91b484cb5aa8fc47 to your computer and use it in GitHub Desktop.
Functional Scala ... tail recursive computation of fib number
//tail recursive fibonacci numbers
def fib(n: Int) = {
@scala.annotation.tailrec
def fibImpl(a:Int, nxt: Int, res: Int) : Int =
a match {
case 0 => res
case _ => fibImpl(a-1, nxt+res, nxt)
}
fibImpl(n, 1, 0);
}
(0 until 10) map fib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment