Prolog W2

Prolog Practical Session - Week 2


The file simp.pl (on unix ~ryang/public_html/prolog/code/simp.pl) contains a Prolog program simp(E1,E2) where E1 is an input arithmetic expression composed by operator '+' and E2 is an output which is same as E1 expect that some redundant zeroes are removed.

For example,

?- simp(0+a+b+0+0+1, Ans).

produces a solution

Ans = a+b+1

?- simp((0+a)+(0+0), Ans).

produces a solution

Ans = a

Extend this program so that it can deal with expressions containing '*' as well as '+'. Moreover, please make sure it can deal with the following two cases:

x*0 (or 0*x) should be simplified to 0

x*1 (or 1*x) should be simplified to x

Test your program by using the following queries:

?- simp(a+b+3*c+0+1*d+0+0, R). (answer should be a+b+3*c+d)

?- simp(1*(a+0)+0+d*1+c*0, R). (answer should be a+d)

?- simp((a+b)*(0+0*d), R). (answer should be 0)

?- simp(3+1*0+0+a*1*b*1, R). (answer should be 3+a*b)

Finally create three your own queries.

5 marks for the above work.



Submit your code with test results to Rong Yang after you finish the task. This work is worth of 5% this term's assignment.


Rong Yang,