scala> def g(x:Int, f:Int=>Int) = f(x) g: (x: Int, f: Int => Int)Int scala> def pippo(x:Int) = 2*x pippo: (x: Int)Int scala> g(12, pippo) res0: Int = 24 scala> g(12, g) :12: error: type mismatch; found : (Int, Int => Int) => Int required: Int => Int g(12, g) ^ scala> def stampa(x:String) = { | println(x) | } stampa: (x: String)Unit scala> g(5,g(555,pippo)) :13: error: type mismatch; found : Int required: Int => Int g(5,g(555,pippo)) ^ scala> def f(x:Int, y:Int) = if (x>y) x else y f: (x: Int, y: Int)Int scala> f(2,2.3) :12: error: type mismatch; found : Double(2.3) required: Int f(2,2.3) ^ scala> def f(x:Int, y:Double) = if (x>y) x else y f: (x: Int, y: Double)Double scala> f(2,1.5) res4: Double = 2.0 scala> def f(x:Int) = 2*x f: (x: Int)Int scala> val f2 = (x:Int) => 2*x f2: Int => Int = scala> f(2) res5: Int = 4 scala> f2(2) res6: Int = 4 scala> val f3 = (x:Int, y:Int) => x+y f3: (Int, Int) => Int = scala> f3 res7: (Int, Int) => Int = scala> f(3,6) :12: error: too many arguments for method f: (x: Int)Int f(3,6) ^ scala> f3(3,6) res9: Int = 9 scala> val f2:Int=>Int = (x:Int) => 2*x f2: Int => Int = scala> val f2:Int=>Int = x => 2*x f2: Int => Int = scala> val f2:Int=>Int = 2*_ f2: Int => Int = scala> val g:Int=>Double = x=>2*x g: Int => Double = scala> g(2) res10: Double = 4.0 scala> val g:Int=>Int = x=>2.0*x :10: error: type mismatch; found : Double required: Int val g:Int=>Int = x=>2.0*x ^ scala> def g(x:Int, f:Int=>Int) = f(x) g: (x: Int, f: Int => Int)Int scala> val g:(Int, Int=>Int)=>Int = (x,f)=>f(x) g: (Int, Int => Int) => Int = scala> g(23, x=>2*x) res11: Int = 46 scala> def pippo(x:Int) = 2*x pippo: (x: Int)Int scala> g(23, pippo) res12: Int = 46 scala> g(23, 2*_) res13: Int = 46 scala> val k = (x:Int, y:Int, f:(Int,Int)=>Int) => f(x,y) k: (Int, Int, (Int, Int) => Int) => Int = scala> k(10, 20, (x,y)=>x+y) res14: Int = 30 scala> k(10, 20, _+_) res15: Int = 30 scala> k(10, 20, _-_) res16: Int = -10 scala> k(10, 20, _-_+_) :13: error: missing parameter type for expanded function ((x$1, x$2, x$3) => x$1.$minus(x$2).$plus(x$3)) k(10, 20, _-_+_) ^ :13: error: missing parameter type for expanded function ((x$1: , x$2, x$3) => x$1.$minus(x$2).$plus(x$3)) k(10, 20, _-_+_) ^ :13: error: missing parameter type for expanded function ((x$1: , x$2: , x$3) => x$1.$minus(x$2).$plus(x$3)) k(10, 20, _-_+_) ^ scala> (x:Int)=> x*x res18: Int => Int = scala> _*_ :11: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$times(x$2)) _*_ ^ :11: error: missing parameter type for expanded function ((x$1: , x$2) => x$1.$times(x$2)) _*_ ^ scala> (_:Int)*(_:Int) res20: (Int, Int) => Int = scala> val f:Int=>Int = _*_ :10: error: missing parameter type for expanded function ((x$1, x$2) => x$1.$times(x$2)) val f:Int=>Int = _*_ ^ :10: error: missing parameter type for expanded function ((x$1: , x$2) => x$1.$times(x$2)) val f:Int=>Int = _*_ ^ scala> pippo :12: error: missing arguments for method pippo; follow this method with `_' if you want to treat it as a partially applied function pippo ^ scala> pippo(10) res22: Int = 20 scala> val q = pippo :11: error: missing arguments for method pippo; follow this method with `_' if you want to treat it as a partially applied function val q = pippo ^ scala> val q = pippo _ q: Int => Int = scala> val q:Int=>Int = pippo q: Int => Int = scala> def aggiungiX(x:Int) = (n:Int) => x+n aggiungiX: (x: Int)Int => Int scala> val aggiungiDue = aggiungiX(2) aggiungiDue: Int => Int = scala> val aggiungiCinque = aggiungiX(5) aggiungiCinque: Int => Int = scala> aggiungi aggiungiCinque aggiungiDue aggiungiX scala> aggiungiDue(100) res23: Int = 102 scala> aggiungiCinque(100) res24: Int = 105 scala> var x = 10 x: Int = 10 scala> x = x + 1 x: Int = 11 scala> def componi(f:Int=>Int, g:Int=>Int) = | x => f(g(x)) :14: error: missing parameter type x => f(g(x)) ^ scala> def componi(f:Int=>Int, g:Int=>Int) = | (x:Int) => f(g(x)) componi: (f: Int => Int, g: Int => Int)Int => Int scala> val c = componi(_+1, _*2) c: Int => Int = scala> c(10) res25: Int = 21 scala> componi(_+1, _*2)(10) res26: Int = 21 scala> def componi(f:Int=>Int, g:Int=>Int) = | def composta(x:Int) = f(g(x)) :2: error: illegal start of simple expression def composta(x:Int) = f(g(x)) ^ scala> def componi(f:Int=>Int, g:Int=>Int) = { | def composta(x:Int) = f(g(x)) | composta _ | } componi: (f: Int => Int, g: Int => Int)Int => Int scala> val c = componi(_+2, _*4) c: Int => Int = scala> val f:Int=>(Int=>(Int=>Int)) = x=>y=>z=>x+y+z f: Int => (Int => (Int => Int)) = scala> f(10)(20)(10) res27: Int = 40