//recursive
def sum(f: Int => Int, a: Int, b: Int): Int = {
if(a > b) 0 else f(a) + sum(f, a + 1, b)
}
//non recursive
def sum(f: Int => Int, a: Int, b: Int): Int = {
var res = 0
for(ele <- a to b by 1)
res = res + f(ele)
res
}
def id(i: Int) = i
def sqr(i: Int) = math.pow(i, 2).toInt
def cube(i: Int) = math.pow(i, 3).toInt
sum(id, 1, 10)
sum(sqr, 1, 5)
sum(cube, 1, 4)