A univariate polynomial is an express of the form an*xn + ... a1*x + a0 where n is a natural number and the ai are all numbers (which, for the purposes of this exercise, we will assume are natural numbers as well).
To simplify matters, you can assume that the variable in question in x. You can assume that exponentiation is represented as repeated multiplication, addition is represent using the constructor p, and multiplication is represented using t. And, to make things simpler for you, you can assume that p and t associate to the right. For example, you can write 3x2 + 2x + 1 as p(t(3,t(x,x)),p(t(2,x),1)).
Your first job in this exercise is to define a binary function add that takes two univariate polynomials and returns the result of adding them together. Your second job is to define a binary function mul that takes two univariate polynomials and returns the result of multiplying them together. In both cases, the result should be a univariate polynomial in the form an*xn + ... a1*x + a0. For example, add(p(x,1),p(x,1)) should result in p(t(2,x),2); and mul(p(x,1),p(x,1)) should result in p(t(x,x),p(t(2,x),1)).
Hints. We suggest you define a simplification function simp that simplifies its argument, e.g. converting p(x,x) to t(2,x). And we suggest you use the predefined relations number and symbol to detect whether an expression is a number or a symbol.
Submission details: Paste the rules defining add and mul in the box below and press Submit.