writing recursive lambda functions

  • Thread starter =?iso-8859-1?q?Thorsten_R=FChl?=
  • Start date
?

=?iso-8859-1?q?Thorsten_R=FChl?=

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])
------------------------------
 
F

Francis Avila

Thorsten Rühl wrote in message ...
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.

You need to return the lambda. You've been coding too much lisp. ;)
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.

Because mapp always returns None (the implicit return value), and None isn't
a 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.

I'm not sure why you want to code Python as if it were lisp. This will only
lead to tears. It's also quite a bit slower, because calling is expensive
in Python. (I understand if this is simply an academic exercize, but still,
try to read beyond the formal definitions).

Typical thing you'd do in Python is one line:
[x*x for x in L]

This makes crystal clear what you want to do. I had to puzzle over that
pseudolisp to figure out the big picture.

Replace x*x with whatever you want. The recursive '(cons ((car L) (func
(cdr L))))' pattern in lisp is 'for item in sequence' in Python, and there's
no need to pass a lambda into a function factory.

Also, we already have a map (as you noticed, because of your namespace
clash). So

map(lambda x: x*x, [1,2,3,4])

works. If you want to curry it, you can do so:

def curry(f, *args):
def inner(*args2):
return f(*(args + args2))
return inner

mapQuad = curry(map, lambda x: x*x)
mapQuad([1,2,3,4])
 
J

JCM

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

You're missing a return here:

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

A common mistake if you're coming from lisp (I do it all the time).
 
?

=?iso-8859-1?q?Thorsten_R=FChl?=

Am Wed, 24 Dec 2003 12:33:46 +0000 schrieb JCM:
You're missing a return here:

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

A common mistake if you're coming from lisp (I do it all the time).


Thanks to you both for you´re quick answer...
... hehe obviosly an beginners mistake ;)

Thank you for yur help !!!! :))))

CU

Thorsten
 
?

=?iso-8859-1?q?Thorsten_R=FChl?=

Am Wed, 24 Dec 2003 07:08:41 -0500 schrieb Francis Avila:

You need to return the lambda. You've been coding too much lisp. ;)
i never did bit once i will ;)
I'm not sure why you want to code Python as if it were lisp. This will only
lead to tears. It's also quite a bit slower, because calling is expensive
in Python. (I understand if this is simply an academic exercize, but still,
try to read beyond the formal definitions).
thank you for your help i read your solutions with much interesting. :)))
!!!

grettings

Thorsten
 
T

tutu

??? Is this what you need?
def mapp(f):
lambda l: l and cons(f(car(l)),(mapp(f)(cdr(l))))
print mapp(lambda x: x*x) => None (can't call it)

def mapp(f):
return lambda l: l and cons(f(car(l)),(mapp(f)(cdr(l))))
print mapp(lambda x: x*x) => <function <lambda> at 0x0267ED30>

print "mapQuad([1,2,3,4]): ", mapQuad([1,2,3])
=> mapQuad([1,2,3,4]): [1, 4, 9]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top