Do Exercise 7 of Chapter 7. Put the resulting function in
quick.sml. (It will be easier to first do Exercise 6 and then
change it.)
A polynomial
can be represented as a list of reals
.
Write functions eval, pow and polyToString so
that we can evaluate:
- val p = [1.0,2.0]; (* 2x + 1 *) val p = [1.0,2.0] : real list - eval(p,0.1); val it = 1.2 : real - val p3 = pow(3,p); val p3 = [1.0,6.0,12.0,8.0] : real list - eval(p3,0.1); val it = 1.728 : real - polyToString(p3); val it = "8.0x^3 + 12.0x^2 + 6.0x + 1.0" : string - polyToString [0.0,0.0,~1.0,0.0,0.0]; val it = "0.0x^4 + 0.0x^3 + ~1.0x^2 + 0.0x + 0.0" : string - polyToString [0.0]; val it = "0.0" : string - polyToString []; val it = "0.0" : stringYou will need to use the functions
Real.toString and
Int.toString to get strings for numbers. Don't ``open'' either
the ``Real'' or the ``Int'' structure because it will cause
overloading functions to misbehave.
Do not use if in any of your code for polynomials.
Put the resulting functions in poly.sml.
Our solution uses four additional functions, three of them at the
top-level (useful named functions), and one a helper for the
polyToString function.
Do Exercises 3 and 4 of Chapter 8.
You submit your program work by putting it in the homework4 directory
in your AFS class volume.
You may do all your work in this directory, or you may wish to do your
work in a different directory and copy things when correct into this
directory. In any case, you will lose permission to write things in
this directory after the deadline, which is 4:00pm on Tuesday,
February 24th. In other words, you must be done before lecture
starts.
The homework4 directory should include the following:
quick.smlpoly.sml