Skip to content

Instantly share code, notes, and snippets.

@mrkaspa
Last active August 29, 2015 14:17
Show Gist options
  • Save mrkaspa/9a5ca459d2599c6e520c to your computer and use it in GitHub Desktop.
Save mrkaspa/9a5ca459d2599c6e520c to your computer and use it in GitHub Desktop.
object RecursiveSum extends App {
println(sum(List(1, 2, 3, 10)))
def sum(nums: List[Int]): Int = {
@tailrec
def sumTail(x: Int, xs: List[Int]): Int = xs match {
case Nil => x
case y :: ys => sumTail(x + y, ys)
}
sumTail(0, nums)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment