kotlin: values, functions, currying, partial application and eta-reduction
In the previous post we talked about values, functions, currying, partial application and eta-reduction in haskell. This post shows how the same can be done in kotlin, be it not as nice.
values and functions
val a = 5
Here the symbol a is assigned with the value 5.
val a = 2 + 3
Here the symbol a is assigned with the result of calculating 2 + 3, which also is 5.
fun sum(x: Int, y: Int) = x + y
val a = sum(2, 3)
Here a function sum is defined that takes two arguments and returns the sum of those two numbers.
The value a is assigned to the result of calling sum with arguments 2 and 3, which calculates 2 + 3, which is still 5.
currying
fun addThree(x: Int) = sum(3, x)
Here we define a new function addThree that is defined as calling our earlier sum function with 3 and x.
Kotlin does not support partial application out of the box, so we need to rewrite our sum function.
fun curriedSum(x: Int) = { y: Int -> x + y }
val a = curriedSum(3)(2)
As you can see now partial application is possible for our new curriedSum function.
fun addThree(x: Int) = curriedSum(3)(x)
val a = addThree(2)
Here we have rewritten addThree using curriedSum in curried form. This means that curriedSum is first called with 3 as argument, and the result of that is a new
function that takes only one argument and we call that function with argument x. In general calling a function with many arguments can be seen as calling that function with the first argument, then the result of that with the second argument, etc.
partial application
curriedSum(3) is called a partial application of curriedSum with 3. The result of that partial application is a function that takes only one argument and returns that argument + 3.
eta-reduction
If you think about it, curriedSum(3) is exactly what our addThree function is supposed to do. Because of this we can simplify the definition with a process that is called eta-reduction.
val addThree = curriedSum(3)
val a = addThree(2)
Here we define a new name addThree for the function curriedSum(3) which is the function which takes one argument and returns that argument incremented by 3. As you can see, a will still be 5.
// sum = (+)
This is not possible in kotlin as + is not curried.
// addthree = (+ 3)
This is also not possible in kotlin, as + is still not curried.
summary
It is definitely possible to use these concepts in kotlin, but it is not as nice as in haskell.
tags: haskell