In defence of the two-namespace rule
Colin J. Williams
cjw at connection.com
Sun Jan 23 08:37:14 EST 2000
I recognize that most of this thread deals with deeper issues,
see the later posting by Tim Peters, but I would like to focus
on the list and one of its operations.
Quinn Dunkan wrote:
>
> 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]
>
Wouldn't it be nice if we could write this as:
5 + range(5) or
range(5) + 5?
Are we not complicating things by using '+' to mean addition in
most cases but catenation in the case of a list?
Colin W.
> 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