I am on my way to learning Scala after coming from different programming languages (mostly interpreted). I am doing the following exercise and I get an error.
def sum(f: Int => Int)(a: Int, b: Int): Int = {
def loop(a: Int, acc: Int): Int = {
if (a >= b) acc
else loop(a+1, f(a) + acc)
}
loop(a, 0)
}
sum(x => x * x, 2, 4) //Too many arguments
I can't see what is wrong there?
If you declare your function with multiple parentheses (multiple argument lists), you also have to call it like that (curried form):
sum(x => x * x)(2, 4)
See What's the difference between multiple parameters lists and multiple parameters per list in Scala? for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With