oasis - How to split main and the rest of your code with Ocaml? -
i wondering how achieve following:
- have "business logic" in file
- have main.ml uses business logic
business logic:
type point = {x:float; y:float;} let pi_known = 3.141592653589793238462643383279502884197169399375105820974944592307816406286 let percentage_error pi_estimated = ((pi_known -. pi_estimated) /. pi_known) *. 100.0 let pi_and_error pi error = printf.sprintf "pi's value :: %.10f error rate :: %.10f %.%" pi error let point_to_string { x = x; y = y } = printf.sprintf "%.2f %.2f" x y let gen_point xr yr = {x=xr; y=yr} let distance_between_points p q = (p.x -. q.x) *. (p.x -. q.x) +. (p.y -. q.y) *. (p.y -. q.y) let distance_from_origin c = distance_between_points c (gen_point 0.0 0.0) let count_within ~counter:n = let rec count_within_aux ~counter:n ~within:m = match n, m | 0, m -> m | n, m -> let cc = gen_point (random.float 1.0) (random.float 1.0) in let dist = distance_from_origin cc in match dist | dist when dist <= 1.0 -> count_within_aux ~counter:(n - 1) ~within:(m + 1) | dist when dist > 1.0 -> count_within_aux ~counter:(n - 1) ~within:m | _ -> 0 in count_within_aux ~counter:n ~within:0 let count_within_stepping ~counter:n ~stepping:s = let rec count_within_stepping_aux ~counter:n ~within:m ~acc:acc = match n, m, acc | n, m, acc when n <= 0 -> m | n, m, acc -> let c = count_within s in let pi = ((float_of_int m) /. (float_of_int acc)) *. 4.0 in let r = percentage_error pi in print_endline (pi_and_error pi r); count_within_stepping_aux ~counter:(n-s) ~within:(m+c) ~acc:(acc+s) in count_within_stepping_aux ~counter:n ~within:0 ~acc:0
pi.mli:
(* * point in two-dimensional euclidean space *) type point = {x:float; y:float;} val point_to_string : point -> string val gen_point : float -> float -> point (* * 'euclidean distance or euclidean metric "ordinary" straight-line distance between * 2 points in euclidean space. distance, euclidean space becomes metric space. * associated norm called euclidean norm. * older literature refers metric pythagorean metric.' * https://en.wikipedia.org/wiki/euclidean_distance *) val distance_between_points : point -> point -> float val distance_from_origin : point -> float val count_within : counter:int -> int val count_within_stepping : counter:int -> stepping:int -> int val percentage_error : float -> float val pi_and_error : float -> float -> string
main.ml:
let main () = random.self_init(); let num_iter = sys.argv.(1) in let n = int_of_string num_iter in print_endline ("number of iterations :: " ^ num_iter); let pi_estimated = ((float_of_int (pi.count_within_stepping n (n / 20))) /. (float_of_int n)) *. 4.0 in let r = pi.percentage_error pi_estimated in print_endline (pi.pi_and_error pi_estimated r) let () = main ()
_oasis:
name: pi version: 0.1 synopsis: nope authors: istvan <istvan@mail.tld> license: mit homepage: http://0.0.0.0 oasisformat: 0.4 buildtools: ocamlbuild plugins: meta (0.4), devfiles (0.4) executable "pi" path: src mainis: main.ml compiledobject: best builddepends: str,unix
unfortunatelly when compile , run it returns nothing while if merge main.ml , pi.ml works expected. missing?
updte:
after adding mli file project , changing main.ml suggested @gallais works expected.
it turns out mli files necessary using modules. having src/x.ml , src/x.mli required. if these files present x can referenced main.ml.
Comments
Post a Comment