Guest User

Untitled

a guest
May 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. (* 2008 Jonathan Dehan *)
  2. (* CSE307 Fall w/Stark *)
  3. (* polynomial functions *)
  4.  
  5. (*
  6. Besides readability, writability, and reliability, a programming language can be judged on it's built in library of functions / integration with tools.
  7. 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
  8. editor is made aware of the common libraries advanced debugging is easier.
  9. *)
  10. type poly = real list
  11.  
  12. val pn = []
  13. val p0 = [0.0]
  14. val p1 = [1.0,2.0,3.0]
  15. val p2 = [1.0,1.0]
  16. val p4 = [3.0,5.0,0.0,3.0]
  17.  
  18. fun degree(p)= if length(p)-1 < 0 then 0 else length(p)-1;
  19.  
  20. fun add (nil: real list, nil: real list) = nil
  21. | add (nil: real list, q: real list) = q
  22. | add (p:real list, nil:real list) = p
  23. | add (h::t,h'::t') = (h+h')::add(t,t');
  24.  
  25. (*
  26. fun scale ( r: real, p: real list) = map( fn x: real=>(x*r): real, p: real list);
  27. *)
  28.  
  29.  
  30. fun scale ( r: real, nil: real list) = nil
  31. | scale ( r: real, (h::t): real list ) = r*h::scale(r,t);
  32.  
  33. fun mul ( [], _ ) = []
  34. | mul ( _, [] ) = []
  35. | mul ( h::t, p) = add(scale(h, p), 0.0::mul(t, p));
  36.  
  37. fun toString ( [] ) = ""
  38. | toString (h::t) =
  39. ( if
  40. ( Real.toString(h)="0.0" )
  41. then
  42. ( "" )
  43. else Real.toString(h) ^ (
  44. case degree(t)+1 of
  45. 0 => ""
  46. | 1 => "x + "
  47. | r => "x^" ^ Int.toString(r) ^ " + " )
  48. )
  49. ^ toString(t);
  50.  
  51. (*
  52. fun eval (r: real, []) = []
  53. | eval (r: real, (h::t): real list) =
  54. *)
  55.  
  56. fun map f l
  57.  
  58. degree(pn) = 0;
  59. degree(p0) = 1;
  60. degree(p1) = 3;
  61. degree(p2) = 2;
  62. degree(p4) = 4;
  63.  
  64. add(pn, p0);
  65. add(p1, p2);
  66. add(p2, p4);
  67. add(p1, p4);
  68. add(p2, p0);
  69.  
  70. scale(5.0, pn);
  71. scale(3.0, p0);
  72. scale(1.0, p1);
  73. scale(0.0, p2);
  74. scale(~4.0, p4);
  75.  
  76. mul(pn,p0);
  77. mul(pn,p2);
  78. mul(p0,p4);
  79. mul(p1,p2);
  80. mul(p2,p4);
  81.  
  82. toString(pn);
  83. toString(p0);
  84. toString(p1);
  85. toString(p2);
  86. toString(p4);
Add Comment
Please, Sign In to add comment