On 3/10/06, Edgar Antonio Rodriguez Velazco <[EMAIL PROTECTED]> wrote:
Hi,
Could you please explain this code?.

f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1

This is why lambdas are evil. Officially, they are for creating "anonymous functions"; usually they only succeed in creating obscure unreadable drek. In My Humble Opinion.

Okay - start from the middle and work our way through:

n is your variable that you're passing as an argument to this unnamed function.

say, n were 10.

n-1 is 9
add EITHER:
absolute value of n-1 AND do a recursive call to f on 9 (and then on 8, and then on 7...), multiplied by n
OR add 1.  

if you type it into your interpreter, here's the result:
>>> n = 10
>>> f = lambda n: n-1 + abs(n-1) and f(n-1)*n or 1
>>> print f(n)
3628800

Yuck. I hate reading lambdas. Personally, I think it's buttugly.

Anna

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to