Created
October 21, 2014 03:14
-
-
Save krishnabhargav/6dab91b484cb5aa8fc47 to your computer and use it in GitHub Desktop.
Functional Scala ... tail recursive computation of fib number
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
//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