RosettaCodeData/Task/Find-common-directory-path/OCaml/find-common-directory-path.ml

21 lines
542 B
OCaml
Raw Permalink Normal View History

2023-12-16 21:33:55 -08:00
let rec common_prefix xs ys = match xs, ys with
| x :: xs, y :: ys when x = y -> x :: common_prefix xs ys
| _ -> []
2023-07-01 11:58:00 -04:00
2023-12-16 21:33:55 -08:00
let common_prefix_all = function
| x :: xs -> List.fold_left common_prefix x xs
| _ -> []
2023-07-01 11:58:00 -04:00
2023-12-16 21:33:55 -08:00
let common_ancestor ~sep paths =
List.map Str.(split_delim (regexp_string sep)) paths
|> common_prefix_all
|> String.concat sep
let _ = assert begin
common_ancestor ~sep:"/" [
2023-07-01 11:58:00 -04:00
"/home/user1/tmp/coverage/test";
"/home/user1/tmp/covert/operator";
"/home/user1/tmp/coven/members";
2023-12-16 21:33:55 -08:00
] = "/home/user1/tmp"
end