Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Defines a recursive str_contains that walks the haystack with
String.sub to find a needle substring. Real OCaml's String.contains
only accepts a single char, so this baseline implements its own
substring search to stay portable.
let rec str_contains s sub i =
if i + sl > nl then false
else if String.sub s i sl = sub then true
else str_contains s sub (i + 1)
count_matching splits text on newlines, folds with the predicate.
'the quick brown fox\nfox runs fast\nthe dog\nfoxes are clever'
needle = 'fox'
matches = 3 (lines 1, 2, 4 — 'foxes' contains 'fox')
43 baseline programs total.
18 lines
477 B
OCaml
18 lines
477 B
OCaml
let rec str_contains s sub i =
|
|
let nl = String.length s in
|
|
let sl = String.length sub in
|
|
if i + sl > nl then false
|
|
else if String.sub s i sl = sub then true
|
|
else str_contains s sub (i + 1)
|
|
|
|
let count_matching needle text =
|
|
let lines = String.split_on_char '\n' text in
|
|
List.fold_left (fun acc line ->
|
|
if str_contains line needle 0 then acc + 1
|
|
else acc
|
|
) 0 lines
|
|
|
|
;;
|
|
|
|
count_matching "fox" "the quick brown fox\nfox runs fast\nthe dog\nfoxes are clever"
|