Assignment name: ML polynomial evaluator
作者:互联网
Programming Assignment 1
Assignment name: ML polynomial evaluator
Due: Friday, June 30, 2019, 11:59 PM.
Write a file named ‘p1.sml’ that contains a function named ‘epoly’, which accepts as parameters
a list of real values a0 through an, and a single real value x. The list contains the coefficients of a
polynomial of the form a0 + a1x + a2x
2 + … + anx
n
, where the real x used is the x parameter
passed to your function. Your function must return the value of the polynomial specified by the
parameters passed to it. Note that any or all of the coefficients may be equal to zero (0.0), and
that the list of coefficients itself may be empty. (This actually simplifies your code if you think
carefully about how recursive functions work. Also, ML allows you to deal with cases of empty
lists easily. Note that the value of a polynomial with no coefficients is undefined, but can be
assumed to be zero for simplicity.) You may assume that the correct number of parameters are
passed to the function. The list of real valued coefficients must be the first parameter listed, and
the value for x the next. (A call to the function might look something like this: ‘epoly([1.0,-
polynomial evaluator作业代做、代写Python/c++程序语言作业、代做Java实验作业
2.0,1.0], x);’, or, if a you created a named list, ‘epoly(coeffs, 2.1);’.)
Your solution must use recursion.
Code that does not compile and run will receive no credit.
You must document your code, describing what is done and why. This must include file-heading
documentation as shown in ‘sample.sml’ available on the class D2L content page for this
assignment.
Submit the file ‘p1.sml’ to the handin folder for CS3363 on the department’s csx server using the
following command on csx.
handin cs3363 program1 p1.sml
Note: You may need to provide the path to the file you are submitting as part of the file name, if
the current working directory does not contain that file. Also ‘p1’ uses the digit one not the
lower-case letter L.
Please see the information in the “About Languages” area of the D2L site for information about
ML, and the “Using csx” area for how to run it on the department’s csx server.
Programs that do not include proper documentation will be penalized.
Additional instructor comments
ML is a language with a strong affinity for recursive operations. You should try to find a
recursive solution any time you need to implement an algorithm in ML, since this is often the
most efficient method in ML.
In the case of this assignment, a recursive solution is extremely simple and direct. The key to
finding that solution is to remember that there are several different ways to write a polynomial.
The two ways you see most often are the following.
( ) .
But, there is another way to write the same polynomial, as shown below.
( ) ( ( ( ))) f x a0 x a1 x a2 x
This third format can easily be translated into a recursive function that evaluates a polynomial.
This illustrates a point I often raise (and beat people over the head with on a regular basis): Any
question worth asking is worth rephrasing. The reverse is true as well: Any answer worth having
is worth rephrasing. In rephrasing answers and questions you can often discover the hidden
features of a problem, as well as possible solutions.
With regard to recursion, it is important to understand that recursion and proof by induction are
closely related. Practically any algorithm that has a proof by induction can be implemented as a
recursive algorithm.
Recursive functions must have at least three characteristics.
1. A recursive function must call itself.
2. A recursive function must have at least one parameter that changes with each successive
call to the function.
3. A recursive function must have a stopping condition.
For this assignment, you may wish to use an empty coefficient list as a stopping condition. Thus
a pseudo-code description might be something like this –
real epoly (coefs, x) {
if coefs is empty return 0
else return head-of-coefs + x * (epoly (tail-of-coefs, x))
}
Note that this is pseudo-code, not ML code.
You should feel free to use any online tutorials or samples to complete this assignment.
The solution can be implemented in as few as two quite simple lines of code in ML.
A copy of a draft of a Standard ML book is in the ML folder. Just read the first chapter,
rather than trying to understand the material, and being serious study with chapter 2.
因为专业,所以值得信赖。如有需要,请加QQ:99515681 或邮箱:99515681@qq.com
微信:codehelp
标签:function,code,recursive,Assignment,ML,polynomial,evaluator,must 来源: https://www.cnblogs.com/coffeejava/p/11107625.html