scala-exercises / exercises-scalatutorial

Exercises for the "Functional Programming Principles in Scala", part of the FP in Scala specialized program by EPFL.
Apache License 2.0
163 stars 384 forks source link

Apparent Error in Higher Order Functions section #58

Open erikwright opened 5 years ago

erikwright commented 5 years ago

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> 
balath commented 4 years ago

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)