Skip to content

Commit

Permalink
feat(chapter2): recursive fibonacci
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0pi committed Mar 20, 2016
1 parent d763887 commit 082c6a3
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Chapter2Worksheet.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.annotation.tailrec

def fib(n: Int): Long = {
@tailrec
def fibRec(n: Int, prev: Long, cur: Long): Long = {
if (n < 1) {
prev
} else {
fibRec(n - 1, cur, prev + cur)
}
}
fibRec(n, 0, 1)
}

(0 to 5).map(fib(_))

0 comments on commit 082c6a3

Please sign in to comment.