Averages/Arithmetic mean: Difference between revisions

→‎{{header|OCaml}}: using pattern matching instead of if-then-else, as usual in ocaml
(→‎{{header|Factor}}: simplification)
(→‎{{header|OCaml}}: using pattern matching instead of if-then-else, as usual in ocaml)
Line 589:
These functions return a float:
 
<lang ocaml>let mean_floats xs = [] -> 0. | xs -> List.fold_left (+.) 0. xs /. float_of_int (List.length xs)
if xs = [] then
0.
else
List.fold_left (+.) 0. xs /. float_of_int (List.length xs)
 
let mean_ints xs = mean_floats (List.map float_of_int xs)</lang>
Line 603 ⟶ 599:
float_of_int on every numbers, converting only the result of the addition.
(also when using List.map and the order is not important, you can use
List.rev_map instead to save an internal List.rev.).
Also the task asks to return 0 on empty lists, but in OCaml this case
would rather be handled by an exception.
Anonymous user