Fun with lambda and map

ivo at ivo at
Tue Feb 5 06:36:56 EST 2002

Gerson Kurz <gerson.kurz at> wrote:
: Well, you can:

: IF_ELSE = lambda x:(map(lambda a:a and 1,[x[0]()])[0] and x[1]()) or
: x[2](x[3])
: fac = lambda n:IF_ELSE([lambda:not n,lambda:n==n,lambda
: n:fac(n-1)*n,n])

Unfortunately, this uses and/or and some list operations, which are not
defined in the lambda calculus I presented. Of course, you could define
them in pure lambda, but I don't thing they would have the short-circuit
behaviour you want.

: If you're in a masochistic mood, you can write this in one line:

: fac = lambda n:map(lambda x:(map(lambda a:a and 1,[x[0]()])[0] and
: x[1]()) or x[2](x[3]),[[lambda:not n,lambda:n==n,lambda
: n:fac(n-1)*n,n]])[0]

I'm in an academic masochistic mood, so I don't want to borrow anything
from python (btw, I got a solution by email that is pure lambda, but I
first have to figure out how it works)


Drs. I.R. van der Wijk                              -=-
Brouwersgracht 132                      Amaze Internet Services V.O.F.
1013 HA Amsterdam                                   -=-
Tel: +31-20-4688336                          Linux/Web/Zope/SQL
Fax: +31-20-4688337                           Network Solutions
Web:                    Consultancy
Email:   ivo at                         -=-

More information about the Python-list mailing list