RosettaCodeData/Task/Strip-comments-from-a-string/OCaml/strip-comments-from-a-string-1.ocaml

21 lines
450 B
Text
Raw Permalink Normal View History

2013-04-11 01:07:29 -07:00
let strip_comments str =
let len = String.length str in
let rec aux print i =
if i >= len then () else
match str.[i] with
| '#' | ';' ->
aux false (succ i)
| '\n' ->
print_char '\n';
aux true (succ i)
| c ->
if print then print_char c;
aux print (succ i)
in
aux true 0
let () =
strip_comments "apples, pears # and bananas\n";
strip_comments "apples, pears ; and bananas\n";
;;