为什么当传入空列表时我的排列函数会发出警告?
我的排列函数:
fun perms [] = [[]] | perms (x::xs) = let fun insertEverywhere [] = [[x]] | insertEverywhere (y::ys) = let fun consY list = y::list in (x::y::ys) :: (map consY (insertEverywhere ys)) end in List.concat (map insertEverywhere (perms xs)) end;
输入:
perms [];
输出:
stdIn:813.1-813.9 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
val it = [[]] : ?.X1 list list
有人可以解释为什么类型变量没有被泛化吗?
需要注意的是,权限类型是在输入权限后给出的;所以
perms;
val it = fn : 'a list -> 'a list list
看起来我已经实现了广义变量,至少对我来说是这样。
my permutation function:
fun perms [] = [[]] | perms (x::xs) = let fun insertEverywhere [] = [[x]] | insertEverywhere (y::ys) = let fun consY list = y::list in (x::y::ys) :: (map consY (insertEverywhere ys)) end in List.concat (map insertEverywhere (perms xs)) end;
input:
perms [];
output:
stdIn:813.1-813.9 Warning: type vars not generalized because of
value restriction are instantiated to dummy types (X1,X2,...)
val it = [[]] : ?.X1 list list
Can someone explain why the type vars aren't generalized?
I should note, the type of perms is given after inputting perms; as
perms;
val it = fn : 'a list -> 'a list list
So it looks like I have achieved generalized variables, to me at least.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
空列表是一个特殊列表,其元素可能具有任何类型。当您调用
perms []
时,编译器会对元素的类型感到困惑。您可以使用:或者
编译器会很高兴,因为 in 可以推断列表的特定类型。
Empty list is a special list which could potentially have any type for its elements. When you invoke
perms []
, the compiler is confused about the type of elements. You can either use:or
then the compiler is happy because in can inference a specific type of the lists.