Working with a list in a more ?pythonic? way

Hung Jung Lu hungjunglu at yahoo.com
Mon Apr 5 00:04:36 EDT 2004


Peter Otten <__peter__ at web.de> wrote in message news:<c4p5pc$4t0$04$1 at news.t-online.com>...
> 
> def calcScore(phrase):
>     return reduce(
>         lambda (sigma, last), i: (sigma + soundScoreMatrix[last][i], i),
>         map(lambda c: ord(c)-65, phrase[1:]), (0, ord(phrase[0])-65))[0]

That's it! Great! "Tail call" in functional programming for emulating
loop variables in imperative programming (the "last" variable in this
case.) However, whenever functional languages get to the point of
using tail calls, I think they get into a sad state of affair. It's
basically using functional language to do imperative programming. In
imperative languages you would have a sequence of lines for the
assignments, and in functional language you pile them up as arguments
to the left in the tail call, effectively emulating states. It's like
writing a program horizontally. :)

Hung Jung



More information about the Python-list mailing list