aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--static/src/cosine.ml47
1 files changed, 47 insertions, 0 deletions
diff --git a/static/src/cosine.ml b/static/src/cosine.ml
new file mode 100644
index 0000000..6617ce1
--- /dev/null
+++ b/static/src/cosine.ml
@@ -0,0 +1,47 @@
+type inf_list = {head: float; tail: inf_list lazy_t}
+
+let rec take l = function
+| 0 -> []
+| n -> l.head :: take (Lazy.force l.tail) (n - 1)
+;;
+
+let print_list l = print_endline @@ String.concat "; " @@ List.map string_of_float l
+;;
+
+let rec ones = {head = 1.; tail = lazy ones}
+;;
+
+print_list @@ take ones 10
+
+let rec map l f = {head = f l.head; tail = lazy (map (Lazy.force l.tail) f)}
+;;
+
+let rec zipWith l l' f = {head = f l.head l'.head; tail = lazy (zipWith (Lazy.force l.tail) (Lazy.force l'.tail) f)}
+;;
+
+let rec ints = {head = 1.; tail = lazy (map ints (fun x -> x +. 1.))}
+;;
+
+print_list @@ take ints 10
+
+let rec expSeries = {head = 1.; tail = lazy (zipWith expSeries ints (/.))}
+;;
+
+let evalAt l n x = List.fold_right (fun a acc -> x *. acc +. a) (take l n) 0.
+;;
+
+print_list @@ take expSeries 10;
+print_float @@ exp 2.;
+print_newline ();
+print_float @@ evalAt expSeries 100 2.;
+print_newline ()
+
+let rec cosSeries = {head = 0.; tail = lazy (map (zipWith sinSeries ints (/.)) (fun x -> -.x))}
+and sinSeries = {head = -.1.; tail = lazy (zipWith cosSeries ints (/.))}
+;;
+
+print_list @@ take cosSeries 10;
+print_float @@ sin 2.;
+print_newline ();
+print_float @@ evalAt cosSeries 100000 2.;
+print_newline ()