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"