In defence of the two-namespace rule

Quinn Dunkan quinn at auto.ugcs.caltech.edu
Fri Jan 21 17:33:56 EST 2000


On 21 Jan 2000 15:36:53 GMT, Aahz Maruch <aahz at netcom.com> wrote:
>In article <m3n1q0fzg7.fsf at greebo.nodomain.de>,
>Bernhard Herzog  <herzog at online.de> wrote:
>>
>>curried functions
>
>All right, I've seen it enough times this week, would somebody mind
>giving the Stupid Person's definition of currying?

A curried function always takes one argument.  If it needs more values to
'finish', it returns a closure which will accept the next arg.  In python:

>>> def add(a):
...    return lambda b, a=a: a + b
...
>>> add(4)
<function <lambda> at 100b41f8>
>>> add(4)(5)
9

A simple example of how this can be useful:

>>> map(add(5), range(5))
[5, 6, 7, 8, 9]

Many functional languages such as the ml family and haskell have all functions
curried by default, including +, *, etc. (there are no two-argument
functions... of course, you could pass a _single_ tuple of two elements :))

Hugs> map (5+) [0..4]
[5,6,7,8,9]

Whenever you know some args for a function now, and the rest later, you can
partially apply it and toss the closure around.  It's very useful if your
brain is working in the appropriate mode.



More information about the Python-list mailing list