Sml 在ML中展开列表

Sml 在ML中展开列表,sml,smlnj,Sml,Smlnj,大家好,我正在尝试编写一个函数,可以从任何深度展开列表中的int,例如,如果我有以下bigList:[12[34],[11[1]]我想接收 [(1,12),(2,34),(3,11),(4,1)] 元组中的第一个元素是深度,第二个元素是数字 我写了这段代码: datatype 'a bigList = Empty of unit | Element of 'a | List of 'a bigList list; local fun unfolder( Empty , n )

大家好,我正在尝试编写一个函数,可以从任何深度展开列表中的int,例如,如果我有以下
bigList:[12[34],[11[1]]
我想接收

[(1,12),(2,34),(3,11),(4,1)]
元组中的第一个元素是深度,第二个元素是数字 我写了这段代码:

datatype 'a bigList = Empty of unit
   | Element of 'a
   | List of 'a bigList list;


local
fun unfolder( Empty , n ) = []
 | unfolder( (Element l)::ls, n ) = (n, l)::unfolder( ls, n )
 | unfolder( (List l)::ls, n) = unfolder( l, n + 1)::unfolder(ls, n)

in
fun flat list = unfolder(list, 1)  
end;
每次我收到以下错误时:

Standard ML of New Jersey v110.71 [built: Thu Sep 17 08:50:14 2009]
- datatype 'a bigList = Element of 'a | Empty of unit | List of 'a bigList list
stdIn:9.5-11.69 Error: data constructor Empty used without argument in pattern
stdIn:11.33-11.69 Error: operator and operand don't agree [tycon mismatch]
  operator domain: (int * 'Z) list * (int * 'Z) list list
  operand:         (int * 'Z) list * (int * 'Z) list
  in expression:
    unfolder (l,n + 1) :: unfolder (ls,n)
- 
提前谢谢你的帮助

在模式中未使用参数的数据构造函数为空

您将
Empty
定义为
Empty of unit
,这意味着您需要将其用作
Empty()
,这是毫无意义的。要将其用作
空的
,需要将其定义为
空的
,而不使用单位的

::
的类型是
'a*['a]->['a]
,这意味着左操作数必须是单个元素,右操作数必须是列表。在上面的两个操作数都是一个列表,因此会出现类型错误

要连接两个列表,请使用
@
运算符,而不是

unfolder (l,n + 1) :: unfolder (ls,n)