Homework # 4
due February 23, 11:00 AM

Programming

A polynomial \( a_n x^n + \ldots + a_1 x + a_0 \) can be represented as a list of reals \( [a_0, a_1, \ldots, a_n] \). 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" : string
You 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 in your AFS homework4 folder.

Our solution uses four additional functions, three of them at the top-level (useful named functions), and one a helper for the polyToString function.

Overloading and Polymorphism

Do Exercises 3 and 4 of Chapter 8 (page 131).

Submitting Your Work

Leave poly.sml in your AFS folder. Turn in the book exercises on paper at the beginning of lecture.


About this document



John Tang Boyland 2009-02-16