如何将列表的元素组合成 Ocaml 中的一个元素?

问题描述

我试图在 Ocaml 中将一组整数组合成一个整数。 例如, 输入 - 列表 = [2,3,6,7,9] 所需输出 - 97632。

我知道如何迭代一个列表,但我应该使用什么操作/函数来获得所需的输出

解决方法

每当您将列表转换为另一个值时,您都应该考虑使用 List.fold_leftList.fold_right。它以另一种方式工作,但请考虑以下内容。

# List.fold_left (fun i x -> i ^ string_of_int x) "" [1;2;3;4];;
- : string = "1234"
# 

请参阅 https://caml.inria.fr/pub/docs/manual-ocaml/libref/Stdlib.List.html 处的列表折叠函数文档。