Hi , I want to implement a function, that returns a function with recursive call on itself. An Example: recursive calculation of compound interest
fun interest(end: Double, inc: Double) = {(start: Double, i: Int) ->
if(start >= end) i
else // here i want to call the function
}
a simple function that works now is
fun interest(start: Double, end: Double, inc: Double, i: Int = 0): Int {
if(start >= e) return i
else return interest(start + start * inc / 100,end, inc, i + 1)
}
so the best looking code would be
fun interest(end: Double, inc: Double) = {(start: Double, i: Int = 0) ->
if(start >= end) i
else (start + start * inc / 100, i + 1)
}
val f = interest(2000, 1)
f(1000)
f(500)
…
Here are two things, that are not working right now:
- default value
- recursive call