exercises-scalatutorial icon indicating copy to clipboard operation
exercises-scalatutorial copied to clipboard

Apparent Error in Higher Order Functions section

Open erikwright opened this issue 6 years ago • 1 comments

https://github.com/scala-exercises/exercises-scalatutorial/blob/master/src/main/scala/scalatutorial/sections/HigherOrderFunctions.scala#L126-L138

If I try to run code like this, I get:

Welcome to Scala 2.12.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_162-ea).
Type in expressions for evaluation. Or try :help.

scala> { def f(x1: Int, x2: Int) = x1 + x2 ; f }
<console>:12: error: missing argument list for method f
Unapplied methods are only converted to functions when a function type is expected.
You can make this conversion explicit by writing `f _` or `f(_,_)` instead of `f`.
       { def f(x1: Int, x2: Int) = x1 + x2 ; f }
                                             ^

scala> 

erikwright avatar May 02 '19 19:05 erikwright

Hi @erikwright ;) ! It seems to me that error means that trying to run this code

def f(x1: Int, x2: Int) = x1 + x2 ; f

You're missing the arguments if you want to apply f, which you could fix like this

val nine = f(4, 5)

Or maybe you want to pass f as a parameter to a higher order function, in which case could use f _ or f(_, _) in this way

def sum(f: (Int, Int) => Int, a: Int, b: Int): Int = f(a, b)
sum(f _, 4, 5)

balath avatar Jun 10 '20 19:06 balath