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 evaluation.....

Status
Not open for further replies.

kkmm

Programmer
Apr 18, 2009
1
US
A expression tree has one of the following forms with corresponding values:

1. number(N) where N is a number, with value N.

2. var(V) where V is a Prolog atom. Its value given a list Env of bindings is Z iff binding(V, Z) is a member of Env.

3. binop(Op, Exp1, Exp2) where Op is one of +, -, *, / and Exp1 and Exp2 are expression trees. The value is the result of applying the binary operator Op to the values of the expression trees Exp1 and Exp2.

4. unop(Op, Exp) where Op is one of +, - and Exp is an expression tree. The value is the negation of the value of Exp if Op is -; the value is the value of Exp if Op is +.

Write a Prolog procedure eval(Exp, Env, Z) which succeeds iff Z matches the value of expression tree Exp in environment Env.
Note: Dont use of cuts and in built prolog functions

Example Log:

?- eval(number(32), _, Z).

Z = 32
?- eval(var(a), [binding(b,22), binding(a, 10), binding(a, 11)], Z).

Z = 10 ;

Z = 11 ;

No
?- eval(binop(*, var(a), binop(+, unop(-, var(b)), number(4))),
| [binding(b,22), binding(a, 10), binding(a, 11)], Z).

Z = -180 ;

Z = -198 ;

No
?- eval(binop(*, var(a), binop(+, unop(-, var(b)), number(4))),
| [binding(a, 10), binding(a, 22)], Z).

No
?-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top