Unformatted Attachment Preview
Programming Assignment 0
Assignment name: Proof of Access
Due: Saturday, January 30, 11:59 PM.
Write a file named ‘p0.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 + a2x2 + … + anxn, 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 as this is a “proof of access” to the CS department’s csx
server, the code for epoly is given at the end of this document. Thus, you are not required to
learn how to program in ML, only use the sml interpreter to load and test your code.
The ML solution uses recursion.
Code that does not compile and run will receive no credit. There really is no excuse for code that
does not work when you are given the code.
You must add proper documentation to the code in your submission, describing what is done and
why. This an exercise in reading code. Over a career you may well be expected to understand
programs written in languages you do not know well, if at all. This isn’t as hard as it may sound.
It just takes some practice in analysis and a bit of research. Programs that do not include proper
documentation will be penalized.
Submit the file ‘p0.sml’ to the handin folder for CS3363 on the department’s csx server using
the following command on csx.
You must also submit a screenshot of the session on csx where you have run this code
successfully. This must be a full-window image, not an imaged cropped to just a few lines onscreen.
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.
Please see the information in the “About Languages” area of the Canvas / File folder for
information about ML, and the “Using csx” area for how to run it on the department’s csx server.
Additional instructor comments
ML is a language with a strong affinity for recursive operations. This is true of almost all
functional languages. Though the solution required is given below, read the discussion from
here to that solution. It may well help you to start thinking in the manner best suited to
In the case of this assignment, a recursive solution can be extremely simple and direct. The key
to finding such a solution is to remember that there are several different ways to write a
polynomial. The two ways you see most often are the following.
f ( x ) a0 a1 x a2 x 2 an x n , or
f ( x ) an x n an 1 x n 1 a1 x a0 .
But, there is another way to write the same polynomial, as shown here.
f ( x) a0 x(a1 x(a2 x()))
This third format can easily be translated into a recursive function that evaluates a polynomial.
The first two? Not so much so.
This illustrates a point I often raise (and sometimes beat people over the head with) on a regular
basis: Any question worth asking is worth rephrasing. The reverse is also true: 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 easily
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.
You should feel free to use any online tutorials or samples to complete this assignment, as you
must test your code to ensure that it actually works. (Yes, you can miss typographical errors that
will cost you points. Information about ML will be posted to the Files section of the Canvas site
over the course of the first week of class.)
The solution can be implemented in as few as two quite simple lines of code in ML. Here is one
fun epoly(, x:real) = 0.0 |
epoly(L:real list as h::T, x:real) = h + x * epoly(T, x);
Remember, you do need to ensure that your submission works.