Transforming a multi-argument function into a single-argument, higher-order function.

As we know, when we evaluate, we evaluate the operator first.

add_maker(1)(2)

add_maker(1) is an operator which is another call expression.

This is What is called Currying.
>>> def curry2(f):
        """Return a curried version of the given two-argument function."""
        def g(x):
            def h(y):
                return f(x, y)
            return h
        return g
 
>>> def uncurry2(g):
        """Return a two-argument version of the given curried function."""
        def f(x, y):
            return g(x)(y)
        return f
        
"""We can also use lambda."""
curry2 = lambda f : lambda x : lambda y : f(x,y)
pow_ = curry2(pow)
pow(2)(5) # 32
POW = uncarry2(curry2(pow))(2,5) # 32

Every time we curry a function, we return a function which takes the rest of argument(s).

So we know the effect is same between make_adder and add.

make_adder(3)(5) # 8
add(3,5) # 8

So do f(x,y) and g(x)(y) The difference:

  • One receive an argument and return a function.
  • One receive two arguments and return a value.