In defence of the two-namespace rule

Tim Peters tim_one at email.msn.com
Fri Jan 21 16:01:57 EST 2000


[Aahz Maruch]
> All right, I've seen it enough times this week, would somebody
> mind giving the Stupid Person's definition of currying?

In the extreme, splitting a function of N arguments into a composition of N
1-argument functions.  Commonly used to mean any gimmick for deriving a
function with fewer arguments that holds some number of F's arguments fixed.
For example, binary "+" can be viewed as a function F that takes a number,
and returns a function G that takes a number and returns the sum of that
number with the number passed to F.

Despite the verbal convolution of that example <wink>, it's a very general &
elegant technique used in some functional languages which have nothing *but*
1-argument functions (like Haskell, named in honor of the American logician
Haskell B. Curry -- and "currying" is also named in honor of him).

This can be emulated in Python but it "looks clumsy" here; it's very natural
in languages that support it directly.  For example, if "f" is any
(conceptual) 2-argument function in Haskell,

    g = f 42

defines a 1-argument function "g" such that (switching to Python notation
now!)

    g(x) == f(42, x)

for all x.  Haskell has several shorthand notations for currying of various
sorts.  For example,

    (- 1)

is the function that subtracts one from its argument, while

    (1 -)

is the function that subtracts its argument from 1.  Mix that into the usual
functional stew of higher-order functions and maps etc, and it's a darned
tasty meal!  Not to everyone's tastes, though.

Historical note:  Curry had nothing to do with computers.  He worked in the
foundations of mathematical logic, specifically the branch called
"combinatory logic".  This branch showed that the concept of (roughly
speaking) "variable names" isn't necessary, and, indeed, given all the
gotchas caused by bound and free vrbl names in formal logic, many complex
proofs become a heck of a lot more transparent without them.  An offshoot
showed that this is directly applicable to computational theory as well, and
leads to an amazingly easy and clean (tho not particularly efficient in its
naive form) implementation technique for functional languages, one that does
lazy evaluation and many forms of common subexpression elimination "for
free, by magic".

Raymond Smullyan wrote a strange and wonderful "general interest"
introduction to combinatory logic, in the form of a series of puzzles
involving birds(! when a bird hears another bird's song, it may respond with
a song of its own -- so a bird is a one-argument function over the universe
of songs <wink>).  It ends with a more-than-less rigorous proof of Godel's
incompleteness theorem, phrased in bird terms.

If you enjoy this kind of think (and, honestly, who doesn't <wink>?), check
it out:  "To Mock a Mockingbird".

every-bird-in-the-forest-is-flawed-ly y'rs  - tim






More information about the Python-list mailing list