Text processing/2: Difference between revisions

→‎{{header|OCaml}}: tail recursive call should be outside of try
(→‎{{header|OCaml}}: tail recursive call should be outside of try)
Line 1,649:
 
let strip_cr str =
let last = pred (String.length str) in
if str.[last] <> '\r' then (str) else (String.sub str 0 last)
 
let map_records =
Line 1,658:
aux (e::acc) tail
| [_] -> invalid_arg "invalid data"
| [] -> (List.rev acc)
in
aux [] ;;
Line 1,671:
aux acc tl
| [] ->
(List.rev acc)
in
aux [] ;;
 
let record_ok (_,record) =
let is_ok (_,v) = (v >= 1) in
let sum_ok =
List.fold_left (fun sum this ->
if is_ok this then succ sum else sum) 0 record
in
(sum_ok = 24)
 
let num_good_records =
Line 1,690:
let li = split (regexp "[ \t]+") line in
let records = map_records (List.tl li)
and date = (List.hd li) in
(date, records)
 
Line 1,696:
let ic = open_in "readings.txt" in
let rec read_loop acc =
let lineline_opt = try Some (strip_cr (input_line ic) in)
try
with End_of_file -> None
let line = strip_cr(input_line ic) in
tryin
read_loop ((parse_line line) :: acc)
withmatch End_of_fileline_opt ->with
None -> close_in ic; List.rev acc
| Some line -> read_loop (List.revparse_line line :: acc)
in
let inputs = read_loop [] in
Anonymous user