RosettaCodeData/Task/Dot-product/OCaml/dot-product-2.ml
2024-10-16 18:07:41 -07:00

11 lines
275 B
OCaml

let dot v u =
if Array.length v <> Array.length u
then invalid_arg "Different array lengths";
let times v u =
Array.mapi (fun i v_i -> v_i *. u.(i)) v
in Array.fold_left (+.) 0. (times v u)
(*
# dot [| 1.0; 3.0; -5.0 |] [| 4.0; -2.0; -1.0 |];;
- : float = 3.
*)