如何在 OCaml 中打印列表列表

How to print a list of lists in OCaml

你好,我正在尝试打印我的列表列表,但我无法尝试很多方法,但我找不到解决方案。

我试图将列表列表转换为数组,因为我知道如何打印数组并且我正在使用此函数 let sll_to_saa sll = Array.of_list (List.map Array.of_list sll) 但它不会将列表列表转换为简单数组。它转换为 Array Array.

有人可以帮我吗? :D

要打印列表:let printlist l = List.iter (fun x -> print x) l
打印列表列表:List.iter (fun ll -> printlist ll) l

关于您的函数 sll_to_saa:根据定义,它 return 是一个数组数组。

如果您希望 return 单个数组:您可能希望得到一个列表列表的串联数组。

如果你可以打印一个列表(参见,每个例子:Print a List in OCaml),你可以将一个列表的列表变成一个列表(使用 concat 别名 flatten)和打印列表。

val concat : 'a list list -> 'a list
Concatenate a list of lists. The elements of the argument are all concatenated together (in the same order) to give the result. Not tail-recursive (length of the argument + length of the longest sub-list).