为什么当传入空列表时我的排列函数会发出警告?

发布于 2024-12-13 16:18:58 字数 812 浏览 3 评论 0原文

我的排列函数:

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫性小仙女 2024-12-20 16:18:58

空列表是一个特殊列表,其元素可能具有任何类型。当您调用perms []时,编译器会对元素的类型感到困惑。您可以使用:

> val ps: int list list = perms [];

或者

> val ps = perms ([]: int list);

编译器会很高兴,因为 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:

> val ps: int list list = perms [];

or

> val ps = perms ([]: int list);

then the compiler is happy because in can inference a specific type of the lists.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文