473,394 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

help with recursion on GP project

I'm trying to create a recursive function to evaluate the expressions
within a list. The function uses eval() to evaluate the list. Like a
lisp interpreter but very limited.
What I'm looking for is a function to recursively traverse the list and
provide answers in place of lists, so that ...
Example = ['add', ['sub', 5, 4], ['mul', 3, 2]]
Becomes: Example = ['add', 1, 6]
Becomes: Example = 7
*Functions are defined in the script

The code I currently have, which isn't pretty (bottom), doesn't work
because it doesn't return the value of the evaluated list. But I can't
figure out how to do that. Any help would be greatly appreciated.

Jack Trades
def recursive(tree):
if type(tree[1]) != type([]) and type(tree[2]) != type([]):
eval(a[0]+'('+str(tree[1])+','+str(tree[2])+')')
if type(tree[2]) == type([]):
recursive(tree[2])
if type(tree[1]) == type([]):
recursive(tree[1])
Jan 14 '07 #1
1 1322
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

Jan 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
3
by: Thomas Philips | last post by:
I'm teaching myself Python and never having used recursion and generators, created the simple recursive program shown below and single stepped through it in IDLE with the debugger turned on to get...
5
by: Ioannis Vranos | last post by:
The following code is supposed to print all folder names of a folder but it does not work. Why? Change the folder in main() to a folder suitable for your system so as to test it. I created...
4
by: pauldepstein | last post by:
I am writing code to price some financial options using recursive functions. It compiles but I get a runtime error which is caused by a memory violation. The problem is that the code is much...
24
by: proctor | last post by:
hello, i have a small function which mimics binary counting. it runs fine as long as the input is not too long, but if i give it input longer than 8 characters it gives RuntimeError: maximum...
7
by: Joey | last post by:
VS2005 asp.net 2.0 C# Developing with File System/Cassini instead of IIS (publish to IIS every so often) Hello guys, I have a web app where I am using static variables on many pages to...
4
by: Luke Davis | last post by:
I've been programming in C# for about 3 months now and I've completed a previous project that worked a lot with data, so I know the basics. This is what I need to do, try and see what you would...
10
by: Jonathan Wood | last post by:
I'm taking my first stab at using ASP.NET memberships and could use some help. I'm following along in a book, which recommends I use the Web Application Administration Tool in VS 2005. A...
1
by: teejayem | last post by:
I have created a UserControl within a Class Library Project type. I will be using this as I want the object to perform some ClientSide scripts. I have built the project and copied the DLL to the...
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.