Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too many arguments when I use (...)(...) function

Tags:

scala

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?

like image 264
Andrew Avatar asked Nov 30 '25 02:11

Andrew


1 Answers

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.

like image 177
rolve Avatar answered Dec 02 '25 15:12

rolve