help with recursion on GP project

bearophileHUGS at lycos.com bearophileHUGS at lycos.com
Sun Jan 14 15:04:56 EST 2007


First possible raw solution:

from operator import add, sub, mul, div, neg

def evaluate(expr):
    if isinstance(expr, list):
        fun, ops = expr[0], expr[1:]
        return fun(*map(evaluate, ops))
    else:
        return expr

example = [add, [add, [sub, 5, 4], [mul, 3, 2]], [neg, 5]]
print evaluate(example)

But it's rather slow...

Bye,
bearophile




More information about the Python-list mailing list