fp/lambda question

Jozef jozef at jozef.joz
Sat Apr 13 17:28:02 EDT 2002


"Alex Martelli" <aleax at aleax.it> wrote:
 
> You're clearly using Python 2.1.something and don't have a:
> 
>     from __future__ import nested_scopes
> 
> at the very top of your module.

Right! So my problem was already solved :-)

> I like FP a lot, and I practise it -- but I eschew lambda in Python in
> most cases: it's limited and goes much against the grain of the
> language.  All it boils down to is, give your functions a name -- in
> most cases a well-chosen name will clarify your code.  E.g.,
> 
> def relatively_prime(a, b): return gcd(a, b) == 1

Thats funny, I wanted to do that but I don't know how to use filter with a
function that takes 2 variables. E.g., to get a list if all numbers less
than 10 relatively prime to 3, I would write

	filter(relatively_prime, range(1, 10)) # aaah, where does the 3 go???

So I learned the lambda way. I also looked at Haskell recently, and  there
(relatively_prime 3) is a valid function that takes only 1 argument, so no
problem there. Just a thought: a nice way to get rid of lambda's and save
a little typing would be if for example

	filter(is_even, alist)
and
	filter(is_even(_), alist)

meant the same thing, and in

	filter(relatively_prime(3, _), alist)

the list elements would be inserted at the '_'. It would of course just be
short for

	filter(lambda x: relatively_prime(3, x), alist)

I'm using Python for about a month so I know I can't be making any sense
to real programmers, but anyway :-)

And thx for the advice



More information about the Python-list mailing list