1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module L = BatList
let with_in_file fn f =
let input = open_in_bin fn in
let res = f input in
close_in input;
res
let with_out_file fn f =
let output = open_out_bin fn in
let res = f output in
close_out output;
res
let stddev (l: float list) =
let n, sx, sx2 =
List.fold_left (fun (n, sx, sx2) x ->
(n +. 1., sx +. x, sx2 +. (x *.x))
) (0., 0., 0.) l
in
sqrt ((sx2 -. (sx *. sx) /. n) /. n)
let lines_of_file fn =
with_in_file fn (fun input ->
let res, exn = L.unfold_exc (fun () -> input_line input) in
if exn <> End_of_file then
raise exn
else res
)
let filter_lines_of_file fn p =
L.filter p (lines_of_file fn)