OCaml 从字符串创建一个整数列表

OCaml creating a list of ints from string

我想读一行这样的结构:

7 12 129224 2 124 12 2 51 

并将字符串的元素放入如下所示的 int 列表中:

[7; 12; 129224; 2; 124; 12; 2; 51]

数字可以有任何大小,不考虑负数。这是我目前所拥有的:

let size_s = read_int();; (* size_s is the number of elements within the String. In this case it's 8. *)
let s = read_line();;

我不确定您正在阅读的前 8 个。它没有出现在你的例子中。

但是,这个旧答案显示了一种将字符串转换为整数列表的方法:

这里有一个 int 的多种大小的例子:

List.map int_of_string (Str.split (Str.regexp "[^0-9]+") "123 8 67 4");;
- : int list = [123; 8; 67; 4]