Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. module type Number = sig
  2. type t
  3. val (+) : t -> t -> t
  4. val (/) : t -> t -> t
  5. val of_int : int -> t
  6. val init : unit -> t
  7. end
  8.  
  9. module Make = functor
  10. (N : Number) ->
  11. struct
  12. let sum arr =
  13. let open N in
  14. let r = ref @@ init () in
  15. for i = 0 to (Array.length arr) - 1 do
  16. r := !r + arr.(i)
  17. done;
  18. !r
  19. let ave arr =
  20. let s = sum arr in
  21. N.(/) s (N.of_int @@ Array.length arr)
  22. end
  23.  
  24.  
  25. module Float = struct
  26. type t = float
  27. let (+) x y = x +. y
  28. let (/) x y = x /. y
  29. let init () = 0.0
  30. let of_int x = float x
  31. end
  32.  
  33.  
  34. module FCal = Make(Float)
  35.  
  36. let main () =
  37. let arr = [|2.1; 2.2; 2.3|]
  38. in
  39. Printf.printf "%f\n" @@ FCal.ave arr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement