Skip to content

Instantly share code, notes, and snippets.

@a-nikolaev
Created August 21, 2017 13:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-nikolaev/b6fb099ebb28d7f58e71e51981960920 to your computer and use it in GitHub Desktop.
Save a-nikolaev/b6fb099ebb28d7f58e71e51981960920 to your computer and use it in GitHub Desktop.
let process filename =
let ic = open_in filename in
(* get list of floats from the file *)
let rec read_one_line () =
try
Some
( input_line ic
|> String.split_on_char ','
|> List.map (fun s -> s |> String.trim |> float_of_string) )
with
End_of_file ->
close_in ic;
None
in
(* iterating reading from file *)
let rec next sum_ls =
match read_one_line () with
| Some ls ->
next (List.map2 ( +. ) sum_ls ls)
| None ->
sum_ls
in
(* read the first line *)
match read_one_line () with
| None -> (* exit if the first line is not found *)
()
| Some ls -> (* process the first line *)
next ls |> List.map (string_of_float) |> String.concat "," |> print_endline
let () =
if Array.length Sys.argv < 2 then
print_endline "Exactly one filename must be given."
else
process Sys.argv.(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment