[Tutor] Cannot Understand

Alan Gauld alan.gauld at freenet.co.uk
Sat Mar 11 01:56:58 CET 2006


>> 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. 

I meant to add to my last post that even using lambdas this 
is a weird attempt at a factorial fiunction.

Here's the equivalent from my web tutor on functional 
programming:

>>> factorial = lambda n: ( (n <= 1) and 1) or
...                       (factorial(n-1) * n)

>>> factorial(5)
120

It uses the same basic constructs but much less esoteric trickery.
Hopefully its slightly more readable, but I still wouldn't recommend 
it as a way to write everyday code!

HTH

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list