score:2

Accepted answer

Your moveGraph function should look like this:

def moveGraph(f: Double => Double, x: Double, y: Double): Double => Double =
  p => f(p - x) + y

If you want to use the passed in parameter, you have to name it.

The compiler doesn't understand what _ should stand for. Here is a list of all possible usages for _.

score:1

A fancier way to do it in scala is using function combinators:

def moveGraph(f: Double => Double, x: Double, y: Double): Double => Double  = 
 {p:Double => p - x } andThen f andThen y.+

((-x).+ andThen f andThen y.+ would be even nicer, but it doesn't' work in this case unfortunately, because + is overloaded, and scala gets confused by it).

score:2

Just adding to Mifeet answer, it is not like you have to name parameters. Just the shorthand notation for anonymous functions is quite simple and just expands in its current scope, so

f(_ - x) + y

becomes

f(param => param - x) + y

This is what compiler did, and complained that it doesn't know the type.

If f would expect a function Int => Int it would be a valid usage.

But you want to do something else here and you need to be explicit about your param

param => f(param - x) + y

Related Query

More Query from same tag