Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expression Tree

Status
Not open for further replies.

kilosi

Programmer
Feb 17, 2007
1
US
Background:
I am trying to write an expression in prolog. I have some code that transverses the tree. I just need to figure out how to perform the operators, as they are variables.

For example, we have this:
compute(expTree(+, expTree(const, 10), 5), X).

When it's all said and done, it will add 10 to 5, giving 15 in X.

The problem is that when I go down the tree, I cannot seem to do anything with the + sign, or any other operator for that matter (the const indicates that there are no children anymore).

Question:
To make this an easier question, I will ask this:

If we had this:
math(A, Operator, B, Answer).

Then we could do this:
math(10, +, 20, X).
Which would do:
X = 30.
(10 + 20).

How would we do that without writing math like this:
math(A, +, B, Answer):- Answer is A + B.

Thank you!


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top