Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SML中的类型问题_Sml_Smlnj - Fatal编程技术网

SML中的类型问题

SML中的类型问题,sml,smlnj,Sml,Smlnj,我必须写一个函数来“取消嵌套”一个列表 例如: 输入[7,8]]、[5,9]]、6]>输出(1,7)、(3,8)、(3,5)、(4,9)、(1,6) 我有这个函数,但我不能使用它,因为类型有问题 功能 datatype 'a superList = Elem of 'a | List of 'a superList list; local fun un_nested( [] , n ) = [] | un_nested( (Elem x)::xs

我必须写一个函数来“取消嵌套”一个列表

例如: 输入[7,8]]、[5,9]]、6]>输出(1,7)、(3,8)、(3,5)、(4,9)、(1,6)

我有这个函数,但我不能使用它,因为类型有问题

功能

datatype 'a superList = Elem of 'a
                    |   List of 'a superList list;


local
fun un_nested( [] , n ) = []
 | un_nested( (Elem x)::xs, n ) = (n, x) :: un_nested( xs, n )
 | un_nested( (List x)::xs, n ) = un_nested( x, n + 1) @ un_nested(xs, n)

in
fun flat list = un_nested(list, 1)  
end;
示例

val test = List[List[Elem 2, List[Elem 3]]];
flat(test);
错误

datatype 'a superList = Elem of 'a | List of 'a superList list
val flat = fn : 'a superList list -> (int * 'a) list
val test = List [List [Elem #,List #]] : int superList
superList.SML:16.1-16.11 Error: operator and operand don't agree [tycon mismatch]
  operator domain: 'Z superList list
  operand:         int superList
  in expression:
    flat test

uncaught exception Error
  raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
             ../compiler/TopLevel/interact/evalloop.sml:44.55
             ../compiler/TopLevel/interact/evalloop.sml:296.17-296.20

谢谢您的帮助。

您的函数接受一个超列表值数组,但您尝试只传递一个值

下面是一个使用高级列表函数的示例:

datatype 'a superList = Elem of 'a
                |   List of 'a superList list;

local
fun un_nested( Elem x , n ) = [(n, x)]
 | un_nested( List x, n ) = List.concat (List.map (fn e => un_nested(e, n + 1)) x)

in
fun flat list = un_nested(list, 1)  
end;
如果不想使用高级函数(我不知道原因),可以这样做:

datatype 'a superList = Elem of 'a
                    |   List of 'a superList list;

local
fun un_nested( Elem x , n ) = [(n, x)]
 | un_nested( List [], n ) = []
 | un_nested( List (x::xs), n ) = un_nested(x, n + 1) @ un_nested(List xs, n)

in
fun flat list = un_nested(list, 1)  
end;