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!
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!