ocaml: phase 5.1 csv.ml baseline (split + int_of_string + fold_left)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 23s
Inline CSV-like text:
a,1,extra
b,2,extra
c,3,extra
d,4,extra
Two-stage String.split_on_char: first on '\n' for rows, then on ','
for fields per row. List.fold_left accumulates int_of_string of the
second field across rows. Result = 1+2+3+4 = 10.
Exercises char escapes inside string literals ('\n'), nested
String.split_on_char, List.fold_left with a non-trivial closure body,
and int_of_string. 23 baseline programs total.
This commit is contained in:
12
lib/ocaml/baseline/csv.ml
Normal file
12
lib/ocaml/baseline/csv.ml
Normal file
@@ -0,0 +1,12 @@
|
||||
let sum_second_col text =
|
||||
let lines = String.split_on_char '\n' text in
|
||||
List.fold_left (fun acc line ->
|
||||
let fields = String.split_on_char ',' line in
|
||||
if List.length fields >= 2 then
|
||||
acc + int_of_string (List.nth fields 1)
|
||||
else acc
|
||||
) 0 lines
|
||||
|
||||
;;
|
||||
|
||||
sum_second_col "a,1,extra\nb,2,extra\nc,3,extra\nd,4,extra"
|
||||
@@ -5,6 +5,7 @@
|
||||
"caesar.ml": 215,
|
||||
"calc.ml": 13,
|
||||
"closures.ml": 315,
|
||||
"csv.ml": 10,
|
||||
"exception_handle.ml": 4,
|
||||
"expr_eval.ml": 16,
|
||||
"factorial.ml": 3628800,
|
||||
|
||||
Reference in New Issue
Block a user