On 07/11/11 23:32, Rafael Turner wrote:
Hello,
I am trying to write the map function as a oneliner....
But I would like to make recursive version. Here is what I was thinking:
I can write map as
def pyMap(F,li):
if li == []:
return []
else:
return [F(li[0])] + map2(F, li[1:])
Using the conditional expression it is nearly trivial to
convert your function to a one-liner:
def pyMap(F,li):
return [] if li == [] else [F(li[0])] + pyMap(F,li[1:])
which becomes
pyMap = lambda F, li: [] if li == [] else [F(li[0])] + pyMap(F,li[1:])
HTH,
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor