Averages/Arithmetic mean: Difference between revisions
Content added Content deleted
(→{{header|Factor}}: simplification) |
(→{{header|OCaml}}: using pattern matching instead of if-then-else, as usual in ocaml) |
||
Line 589: | Line 589: | ||
These functions return a float: |
These functions return a float: |
||
<lang ocaml>let mean_floats xs = |
<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> |
let mean_ints xs = mean_floats (List.map float_of_int xs)</lang> |
||
Line 603: | Line 599: | ||
float_of_int on every numbers, converting only the result of the addition. |
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 |
(also when using List.map and the order is not important, you can use |
||
List.rev_map instead to save an internal List.rev |
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 |
Also the task asks to return 0 on empty lists, but in OCaml this case |
||
would rather be handled by an exception. |
would rather be handled by an exception. |