February 15th, 2007, 06:34 PM

Prolog Tree Question
I am trying to write a prolog program for one of my classes. I have a question on how to do something.
I am given this:
Code:
eTree1(expTree('+',
expTree(const, 5),
expTree('*',
expTree(const, 3),
expTree(const, 2)
)
)
).
I have to calculate the answer, which is 11 in this case.
So I wrote this:
Code:
eval(Tree, V): display([Tree]).
display([]):write('End of the line').
display([HT]):write('H is: '), write(H), nl, write('T is: '), write(T),nl, display(H).
This clearly doesn't work. My problem is that I need to somehow get at what Tree is and do the operations on it, like plus or minus.
When I run the above I get this:
? eTree1(T), eval(T, V).
H is: expTree(+, expTree(const, 5), expTree(*, expTree(const, 3), expTree(const, 2)))
T is: []
No
expTree is not defined in the program we have been given but it has been described in comments as follows:
Below are 3 structures that representation expression trees using Prolog.
(Op is any Prolog operator.)
expTree(Op,Lt,Rt).
expTree(const,Value).
expTree(Op,T).
My question:
So I'm not sure if I have to write expTree or if it is somewhere in the depths of Prolog itself.
Then I have to figure out how to go down the expTree. The only way I know how to go down things is with lists. But this is not a list. So I'm not really sure where to begin and what to be looking at. Once I am pointed in a right direction the I think I will be able to make some headway.
Thank you for any help.