python: 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 python, be it not as nice.
values and functions
a = 5
Here the symbol a
is assigned with the value 5.
a = 2 + 3
Here the symbol a
is assigned with the result of calculating 2 + 3, which also is 5.
def sum(x,y):
return x + y
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
def addThree(x):
return sum(3,x)
Here we define a new function addThree
that is defined as calling our earlier sum
function with 3 and x
.
Python does not support partial application out of the box, so we need to rewrite our sum
function.
def curriedSum(a):
return lambda b: a + b
a = curriedSum(3)(2)
As you can see now partial application is possible for our new curriedSum
function.
def addThree(x):
return curriedSum(3)(x)
a = addThree(2)
Here we have rewritten addThree
using curriedSum
in curried form. This means that sum
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.
addThree = curriedSum(3)
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 python as +
is not curried.
# addthree = (+ 3)
This is also not possible in python, as +
is still not curried.
summary
It is definitely possible to use these concepts in python, but it is not as nice as in haskell.
tags: haskell