I am trying to use Prolog to implement the polynomial multiplication that multiplies two polynomials. Below is the code given in SML, but I need it in Prolog. A test solution for SML is poly_mult([1.0, 5.0, 1.0], [3.0, ~10.0, 15.0]); and will return val it = [3.0,5.0,~32.0,65.0,15.0] : real list I tried to write the code in Prolog but what I have is not correct. Can anyone help? Thanks!
IN SML
fun poly_add (M,nil) = M
| poly_add (nil,N) = N
| poly_add ((m:real)::mr, n::nr) = (m+n)::poly_add(mr,nr);
fun scalar_mult (nil,m) = nil
| scalar_mult((m:real)::mr,n) = (m*n)::scalar_mult(mr,n);
fun poly_mult (M,nil) = nil
| poly_mult (M,n::nr) = poly_add (scalar_mult(M,n), 0.0::poly_mult(M,nr));
WHAT I HAVE IN PROLOG
poly_add(Constant,[],Constant) :- !.
poly_add([],Constant2,Constant) :- !.
poly_add([Head1 | Head2], [Tail | Tail2], [HeadSum |TailSum]) :-
HeadSum is Head + Tail,
poly_add(Head2, Tail2, TailSum).
scal_mult([],Head,[]) :- !.
scal_mult([Head | Head2], [Tail], [HeadMult | TailMult]) :-
HeadMult is Head * Tail,
scal_mult(Head2, Tail, TailMult).
poly_mult(Constant,[],[]) :- !.
poly_mult([Constant], [Tail | Tail2]) :-
poly_add(scal_mult([Constant, Tail]), [0 | poly_mult] ([Constant, Tail2]).