Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (* 2008 Jonathan Dehan *)
- (* CSE307 Fall w/Stark *)
- (* polynomial functions *)
- (*
- Besides readability, writability, and reliability, a programming language can be judged on it's built in library of functions / integration with tools.
- Although these seem like they would fall in the categories of writability, since a writable language lets a programmer quickly fill in the blanks for necessary functions, when a good
- editor is made aware of the common libraries advanced debugging is easier.
- *)
- type poly = real list
- val pn = []
- val p0 = [0.0]
- val p1 = [1.0,2.0,3.0]
- val p2 = [1.0,1.0]
- val p4 = [3.0,5.0,0.0,3.0]
- fun degree(p)= if length(p)-1 < 0 then 0 else length(p)-1;
- fun add (nil: real list, nil: real list) = nil
- | add (nil: real list, q: real list) = q
- | add (p:real list, nil:real list) = p
- | add (h::t,h'::t') = (h+h')::add(t,t');
- (*
- fun scale ( r: real, p: real list) = map( fn x: real=>(x*r): real, p: real list);
- *)
- fun scale ( r: real, nil: real list) = nil
- | scale ( r: real, (h::t): real list ) = r*h::scale(r,t);
- fun mul ( [], _ ) = []
- | mul ( _, [] ) = []
- | mul ( h::t, p) = add(scale(h, p), 0.0::mul(t, p));
- fun toString ( [] ) = ""
- | toString (h::t) =
- ( if
- ( Real.toString(h)="0.0" )
- then
- ( "" )
- else Real.toString(h) ^ (
- case degree(t)+1 of
- 0 => ""
- | 1 => "x + "
- | r => "x^" ^ Int.toString(r) ^ " + " )
- )
- ^ toString(t);
- (*
- fun eval (r: real, []) = []
- | eval (r: real, (h::t): real list) =
- *)
- fun map f l
- degree(pn) = 0;
- degree(p0) = 1;
- degree(p1) = 3;
- degree(p2) = 2;
- degree(p4) = 4;
- add(pn, p0);
- add(p1, p2);
- add(p2, p4);
- add(p1, p4);
- add(p2, p0);
- scale(5.0, pn);
- scale(3.0, p0);
- scale(1.0, p1);
- scale(0.0, p2);
- scale(~4.0, p4);
- mul(pn,p0);
- mul(pn,p2);
- mul(p0,p4);
- mul(p1,p2);
- mul(p2,p4);
- toString(pn);
- toString(p0);
- toString(p1);
- toString(p2);
- toString(p4);
Add Comment
Please, Sign In to add comment