Created
August 21, 2017 13:53
-
-
Save a-nikolaev/b6fb099ebb28d7f58e71e51981960920 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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