[Tutor] Cannot Understand
annaraven at gmail.com
Fri Mar 10 20:30:52 CET 2006
On 3/10/06, Edgar Antonio Rodriguez Velazco <edgar.antonio.rv at gmail.com>
> 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
say, n were 10.
n-1 is 9
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)
Yuck. I hate reading lambdas. Personally, I think it's buttugly.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor