Python and Ruby

Nobody nobody at nowhere.com
Sun Jan 31 21:43:03 CET 2010


On Sun, 31 Jan 2010 03:01:51 -0800, rantingrick wrote:

>> That's also true for most functional languages, e.g. Haskell and ML, as
>> well as e.g. Tcl and most shells. Why require "f(x)" or "(f x)" if "f x"
>> will suffice?
> 
> yuck! wrapping the arg list with parenthesis (python way) makes the most
> sense. Its to easy to misread somthing like this
> 
> onetwothree four five six
> 
> onetwothree(four, five, six) #ahhh... plain english.

Note: Functional languages allow:

	f (a,b,c)

but that means something else, i.e. calling a function with a tuple as its
argument (in functional languages, a function always has exactly one
argument).

The syntax:

	f a b c

is equivalent to the Python expression:

	f(a)(b)(c)

i.e. each argument is applied in turn, with all applications except the
last yielding a function.

Defining f as:

	f a b c = <expression>

is shorthand for:

	f = \a -> (\b -> (\c -> <expression>))

or, in Python syntax:

	f = lambda a: (lambda b: (lambda c: <expression>))

This style (known as Currying, or a Curried function, after the
mathematician Haskell Curry) is common in functional languages, as it
allows you to partially apply functions, e.g.:

	map (f a b) someList

whereas an uncurried function would require a lambda expression:

	map (\c -> f (a,b,c)) someList

IOW, while the uncurried form is allowed, it has no advantages and one
disadvantage, so it's seldom used (and where it is used, it's normally
a case of a function whose sole argument is a naturally-occurring tuple,
rather than one which is constructed simply to satisfy the mechanics of
a function call).

Partial application is common enough that Haskell supports it for infix
operators as well, e.g.

	map (/ 2) someList	-- (/ 2) => \x -> x / 2, i.e. halve
	map (1 /) someList	-- (1 /) => \x -> 1 / x, i.e. reciprocal

If it was common-place to use Curried functions and partial application in
Python, you'd probably prefer "f a b c" to "f(a)(b)(c)" as well.




More information about the Python-list mailing list