Hello, I am trying to write the map function as a oneliner. I currently have implement map as a list comprehension: map = lambda F,li: [F(x) for x in li]
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:]) I can logical encode an if-then-else structure as follows: Let Q be the result of the following IF,THEN, ELSE conditional structure: If(S): Then: A Else: B We can implement this only using logical constructors. Q = (A AND S) OR (B AND NOT S) where: S is the input for the If condition, A is the input to Then subexpression B is the input for the Else subexpression Q is the output of the entire If-Then-Else expression. So, I tried: def pyMap2(F, li): def ifFun(P,eq): return li == [] return ([] and ifFun(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) But it's no use. pyMap works fine but pyMap2 does not. I saved the above code as map.py and ran it in the interpreter. >>> import map >>> map.pyMap2(lambda x: x + 2, [1,2,3]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) File "map.py", line 15, in pyMap2 return ([] and fIF(li,[])) or ([F(li[0])] + pyMap2(F, li[1:]) and not []) IndexError: list index out of range Thank you for your help, Rafael _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor