Skip to content

Instantly share code, notes, and snippets.

@refi64
Last active August 29, 2015 14:21
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 refi64/3ca06336634f8fa19d9e to your computer and use it in GitHub Desktop.
Save refi64/3ca06336634f8fa19d9e to your computer and use it in GitHub Desktop.
Felix entry for "Four MLs (and a Python)"
fun add_to(totals: list[float])(values: list[float]) =>
if values.len != 1.size then let n = totals.len in match n with
| 0uz => values
| $(values.len) => (add of (float^2)).map $ totals.zip2 values
// this ugliness is because of a bug in Felix that generates invalid C++ code
// see https://github.com/felix-lang/felix/issues/71
| _ => #(fun () = { raise "Inconsistent-length rows"; return #list[float]; })
endmatch else totals;
fun add_line_to(totals: list[float])(line: string) =>
totals.add_to $ (float of string).map $ split (line, ',');
fun sum_file(filename: string) =>
add_line_to.fold_left #list[float] $ split (filename.load, '\n');
proc sum_and_print_file(filename: string) {
res := (str of float).map $ sum_file filename;
println $ fold_left (fun (a:string)(b:string) => a+","+b) res.head res.tail;
}
match #System::args with
| Cons (_, Cons (filename, Empty)) => sum_and_print_file filename;
| _ => raise "Exactly 1 filename must be given";
endmatch;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment