Skip to content

Instantly share code, notes, and snippets.

@cgopalan
Created September 8, 2011 17:52
Show Gist options
  • Save cgopalan/1204088 to your computer and use it in GitHub Desktop.
Save cgopalan/1204088 to your computer and use it in GitHub Desktop.
Scala Tail Recursive Sum
object FirstClassFunctions {
def sum(f: Int => Int)(a: Int, b:Int): Int = {
def iter(a: Int, result: Int): Int =
if (a > b) result
else iter(a + 1, f(a) + result)
iter(a,0)
}
def main(args: Array[String]) {
println("Sum is: " + sum(x => x)(1,10))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment