Back to questions
JuniorTheoryCommon
What does the program print?
fun main() {
val double = { x: Int -> x * 2 }
val inc: (Int) -> Int = { it + 1 }
println(double(inc(3)))
}8
- Expecting
7: reading the line left to right. The inner call runs first:inc(3)gives4, and only thendouble(4)gives8. - Expecting
itto need a declaration somewhere: a lambda with one parameter gets the nameitfor it, so{ it + 1 }means the same as{ x -> x + 1 }.
- What does
println(inc(double(3)))print, and why does it differ? - Why does
val double = { it * 2 }not compile, whileincdoes?