Operators as functions

Peter Nuttall p.s.nuttall at dur.ac.uk
Tue Dec 21 00:36:58 CET 2004


On Monday 20 Dec 2004 22:44, Anders Andersson wrote:
> Hello
>
> I want to concatinate (I apologize for bad English, but it is not my
> native language) a list of strings to a string. I could use (I think):
>
> s = ""
> map(lambda x: s.append(x), theList)
>
> But I want to do something like (I think that the code above is clumsy):
>
> s = reduce("concatinating function", theList, "")
>
> And here is the questions: What to replace "concatinating function"
> with? Can I in some way give the +-operator as an argument to the reduce
> function? I know operators can be sent as arguments in Haskell and since
> Python has functions as map, filter and listcomprehension etc. I hope it
> is possible in Python too. In Haskell I would write:
>
> foldr (++) []
>
> Thank you for answering!
>
> --
> Anders Andersson

I would have the reduce like this,

string=reduce(lambda x, y: x+y, TheList)

In this case you have a lambda function to do the expression, but if you are 
like the other haskell coders I know, you know more about lambda functions 
than I do. 

Pete 



More information about the Python-list mailing list