Assignment versus binding

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Oct 6 00:57:15 EDT 2016


Chris Angelico wrote:
> Hence my
> query about how variadic functions and automatic currying work - how
> does it know whether to curry or run?

Calling it "automatic" was probably a bad choice of words.
I don't mean to imply that Haskell goes around currying
things behind your back when you don't want it to.

Rather, Haskell provides a very concise syntax for
defining and using curried functions -- so concise that
it's easier to use it than not, so it tends to get used
by default even when currying isn't strictly necessary.

The Haskell definition

f x y z = x + y + z

is equivalent to the Python definition

f = lambda x: lambda y: lambda z: x + y + z

To call it, you write

f 1 2 3

Function application associates to the left, so that's
equivalent to

((f 1) 2) 3)

or in Python,

f(1)(2)(3)

If you don't want currying, there are various options.
For example, you could define it to take a tuple:

f (x, y, z) = x + y + z

which is equivalent to the Python

def f(args):
   x, y, z = args
   return x + y + z

Then you would call it as

f (1, 2, 3)

i.e. construct a tuple of values and then pass it as the
single argument.

For a variable number of arguments, you could use a list
instead of a tuple, but again you would need to explicitly
construct the list when calling.

I hope you can see from this that there's no confusion over
"whether to curry or run". It all depends on how you define
the function.

-- 
Greg



More information about the Python-list mailing list