so I have to write a small program in SML ->>
a file named ‘p0.sml’ that contains a function named
epoly, which accepts as parameters alistofrealvaluesa0throughan, and a single real valuex. The list contains the coefficients of a polynomial of the forma0 + a1x + a2x 2 + … + anx n, where the realxused is thexparameter passed to your function. Your implementation must accept the list of coefficients as the first parameter and the value ofxas the second. Your function must return the value of the polynomial specified by the parameters passed to it.
this is what I have so far but it won't compile because of a syntax error with as. "Error: syntax error found at AS". If you have any pointers that would be greatly appreciated.
fun epoly([], x:real) = 0.0
= epoly(L:real list as h::T, x:real) = h + (x * epoly(T, x));
It looks like you have a typo. Your second
=should be a|.There is, further, no need to specify types. Your SML compiler can infer the types from data presented. Along with removing unnecessary bindings, this can be reduced to:
From
fun epoly([], _) = 0.0we knowepolywill take a tuple of a list and some type and returnreal.From:
We know that
xis being multiplied by areal, soxmust bereal. And sincehis being added to areal, it must be areal, so the entire list is areal list.Thus the type of
epolycan be inferred correctly to bereal list * real -> real.