hakobe (owner)

Revisions

gist: 140855 Download_button fork
public
Public Clone URL: git://gist.github.com/140855.git
Embed All Files: show embed
sum.scala #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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))