如何在 ML 中定义新的数据类型,假设 newList,它可以包含元素 'a(int、real 等)的列表、嵌套列表,例如:如果我的类型 'a 是 int,值可能是:[1],[1,[4]],[1,[5],[[5]]]等提前感谢您的帮助
已编辑 对不起,示例中没有 1,不同的 int 列表,我将其删除
你不能。列表中的所有元素必须属于同一类型。在您的示例中,有些元素是int
type ,有些是int list
.
你可以创建类似这棵树的东西,但语法不太好用:-)
datatype 'a multiList = Empty
| List of 'a multiList list
| E of 'a;
val x = List [E 1, List [E 1, List [E 4]], List [E 1, List [E 5]]];
datatype 'a multiList = E of 'a
| List of 'a multiList list
你的例子是:
List [E 1]
List [E 1, List [E 4]]
List [E 1, List [E 5], List [List [E 5]]]