How to add polynoms in Prolog?
===

I have the following task: 

> Write a method that will add two polynoms. I.e 0+2*x^3 and 0+1*x^3+2*x^4 will give 0+3*x^3+2*x^4.

I also wrote the following code:

    add_poly(+A1*x^B1+P1,+A2*x^B2+P2,+A3*x^B3+P3):-
	B1=B2,
	B3 = B2,
	A3 is A1+A2,
	add_poly(P1,P2,P3);
	B1<B2,
	B3=B1,
	A3=A1,
        add_poly(P1,+A2*x^B2+P2,P3);
	B1>B2,
	B3=B2,
	A3=A2,
	add_poly(+A1*x^B1+P1,P2,P3).
    add_poly(X+P1,Y+P2,Z+P3):-
    	Z is X+Y,
    	add_poly(P1,P2,P3).

My problem is that I don't know how to stop. I would like to stop when one the arguments is null and than to append the second argument to the third one. But how can I check that they are null?
Thanks. 


