[Solved] Convert a char list list into a char string list in OCAML


Here is a very compact implementation:

let to_str_list =
  List.map (fun cs -> String.concat "" (List.map (String.make 1) cs))

It looks like this when your call it:

# to_str_list [['h';'e';'l';'l';'o'];['w';'o';'r';'l';'d']];;
- : string list = ["hello"; "world"]

Update

If you want to suppress empty strings at the outer level you can do something like this:

let to_str_list css =
  List.map (fun cs -> String.concat "" (List.map (String.make 1) cs)) css |>
  List.filter ((<>) "")

It looks like this when you call it:

# to_str_list [['h';'e';'l';'l';'o']; []; ['w';'o';'r';'l';'d']];;
- : string list = ["hello"; "world"]

1

solved Convert a char list list into a char string list in OCAML