如何用OCAML将主和代码的其余部分拆分



我想知道如何实现以下操作:

  • 文件中有所有"业务逻辑"
  • 有一个使用该业务逻辑的main.ml

业务逻辑:

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 with
      | 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 with
          | 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 with
      | 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 a 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 is the "ordinary" straight-line distance between
 * two points in Euclidean space. With this distance, Euclidean space becomes a metric space.
 * The associated norm is called the Euclidean norm.
 * Older literature refers to the metric as 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

不幸的是,当我对此进行编译并运行它时,如果我合并main.ml和pi.ml,它将无法返回,它可以按预期工作。我想念什么?

updte:

将MLI文件添加到项目并更改main.ml之后,正如@Gallais所建议的那样,它可以按预期工作。

事实证明,使用模块是必需的。需要具有SRC/X.ML和SRC/X.MLI。如果存在这些文件,则可以从main.ml。

引用x

相关内容

  • 没有找到相关文章

最新更新