writing recursive lambda functions

Thorsten Rühl ask_me at gmx.de
Wed Dec 24 06:31:51 EST 2003


Hi there,

at first i have to say i am very new in python so i have a few problems to
get along with some things. 

My first problem i can´t handle is to write a recursive lambda function:
the formal definition is 

letrec map(f) = lambda l. if != NIL then NIL 
       else cons(f(car(l)), map(f)(cdr(l)))

after i realised that i can´t use if clauses in lambda definition i tried
to convert the if line in an correspondending boolean operator line: 

def mapp(f):
    lambda l: l and cons(f(car(l)),(mapp(f)(cdr(l))))

but it doesn´t work.

if i try to use the result in the following way i get an error: object not
callable but i don´t understand why it is not callable. 

So i hope this is question is not to nooby and i did´nt waste your time.

But i really need a little help with this. 

cu

Thorsten 


The whole:
--------------------------------------
def cons(a,l):
        return [a]+l

def car(x):
        return x[0]

def cdr(x):
        return x[1: ]
        
def mapp(f):
    lambda l: l and cons(f(car(l)),(mapp(f)(cdr(l))))

mapQuad = mapp(lambda x: x*x)

print "mapQuad([1,2,3,4]): ", mapQuad([1,2,3])
------------------------------






More information about the Python-list mailing list