Skip to content

Instantly share code, notes, and snippets.

@dpzmick
Created October 17, 2014 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpzmick/b2114287a9b18f416ed3 to your computer and use it in GitHub Desktop.
Save dpzmick/b2114287a9b18f416ed3 to your computer and use it in GitHub Desktop.
let rec read_inp () =
try
let i = read_float() in
i::(read_inp ())
with End_of_file -> []
;;
(* todo tail recursive *)
let rec fact = function
| 1 -> 1
| n -> n * (fact (n-1))
;;
(* creates a function f(x) that approximates e^x to n terms *)
let rec series = function
| 0 -> (fun x -> 1.0)
| n -> (fun x ->
let my_term = x ** (float n) /. (float (fact n)) in
(my_term +. ((series (n-1)) x)))
;;
(* make function that will approx to 10 terms (0..9) *)
let expansion = series 9;;
let _ = ignore (read_int ());
List.iter
(fun f -> Printf.printf "%.4f" (expansion f); print_newline ();)
(read_inp ());;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment