1

how do I pass different parameters for my function inside a higher order function. my code below.

val sum = (a: Int, b:Int) => a + b val x = 7 val y = 9 def sq (x: Int, y: Int, f: (Int, Int)=> Int): Int = { x + y * f(a,b) } println(sq(x,y,sum(2,3))) 

This doesn't work and throws an error that 'a' is not defined.

4
  • What is the point of passing a Function if the method receiving it doesn't know how to call it and rather is the user the one to call it? Like why not just asking for: def sq (x: Int, y: Int, z: Int): Int = x + y +z? Commented Aug 3, 2021 at 17:17
  • 1
    a and b are the names to be used inside the function sum. You are going to pass values x, y as a, b; hence x and y will take place of a and b in function call. so your sq will be x + y * f(x, y) (if you want square then (x + y) * f(x, y)) and you will call sq as sq(x, y, sum). Actually you don't even need to define those val x = 7 and val y = 9. You can directly use sq(7, 9, sum). Commented Aug 3, 2021 at 17:17
  • 1
    Can you define with words what sq should do? How it should use f? Commented Aug 3, 2021 at 17:40
  • 1
    Also note that you are mixing x and y as method parameter names and as variables defined outside the method, this is confusing to understand what you really want to do Commented Aug 3, 2021 at 18:28

3 Answers 3

2

The parameter list for sq looks ok. The call to sq isn’t right, the function would get evaluated before it gets passed in. Assuming it got by the compiler, which of course it doesn’t.

Passing in a function does not mean its arguments come along for the ride. Passing in a function means the caller may not know what arguments to use or even if the function should be called. The sq function has to supply arguments when it calls it. a and b mean nothing outside the definition of sum. sq could supply x and y as arguments to sum.

Sign up to request clarification or add additional context in comments.

Comments

2

Just as an addition to @Nathan answer. This example would make sense:

val sum = (a: Int, b:Int) => a + b def sq (x: Int, y: Int, f: (Int, Int)=> Int): Int = { x + y * f(x, y) } val x = 7 val y = 9 println(sq(x,y,sum)) 

This would be evaluated in your sgfunction like: 7 + 9 * sum(7, 8)

I hope this makes sense.

Comments

2

In addition to the @Nathan Hughes answer, if you want to pass the different argument to the sum function on each call you need to change your sq method like

def sq (x: Int, y: Int, f: (Int, Int)=> Int): (Int, Int)=> Int = { x + y * f(_: Int, _:Int) } 

Insted of expecting the int from the sq method return partially applied function of type (Int, Int) => Int from `sq' then pass other argument to that function like,

sq(10, 20, sum)(1, 2) // 1st approach //2nd approach would be //You can hold the function in another variable and call that function with other arguments val partialSum = sq(10,20, sum) partialSum(1, 2) 

and you will get your result.

Or if you still want that sq method should return Int, you can define your sq method like

def sq (x: Int, y: Int, f: (Int, Int)=> Int)(a:Int, b:Int):Int = { x + y * f(a, b) } scala> sq(10,20, sum)(1,2) res2: Int = 70 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.