Skip to content

Instantly share code, notes, and snippets.

@hakobe
Created July 5, 2009 06:04
Show Gist options
  • Select an option

  • Save hakobe/140855 to your computer and use it in GitHub Desktop.

Select an option

Save hakobe/140855 to your computer and use it in GitHub Desktop.
def iteration(op: (Double, Double) => Double, init: Int)(f: Int => Double)(a: Int, b:Int) :Double = {
def iter(a: Int, result: Double) :Double = {
if (a > b) result
else iter(a+1, op(f(a),result))
}
iter(a,init)
}
//def sum(f: Int => Double)(a: Int, b: Int) :Double = {
// def iter(a: Int, result: Double) :Double = {
// if (a > b) result
// else iter(a+1, f(a)+result)
// }
// iter(a,0)
//}
val sum = iteration( (x, y) => x + y, 0) _
println(sum(x => x)(0,10))
//def product(f: Int => Double)(a: Int, b: Int) :Double = {
// def iter(a: Int, result: Double) :Double = {
// if (a > b) result
// else iter(a+1, f(a)*result)
// }
// iter(a,1)
//}
val product = iteration( (x ,y) => x * y, 1) _
println(product(x => x)(1,10))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment