我想知道为什么 Haskell 接受这个
perms xs = [ x:y | i <- [0..(length xs - 1)], x <- [xs!!i], y <- perms (takeOut i xs)]
但不会接受:
perms xs = [ x:(perms y) | i <- [0..(length xs - 1)], x <- [xs!!i], y <- (takeOut i xs)]
它提示说
[1 of 1] 编译 Main(abc.hs,已解释)
Occurs check: cannot construct the infinite type: t = [t] Expected type: t -> [t] Inferred type: [t] -> [[a]] In the second argument of `(:)', namely `(perms y)' In the expression: x : (perms y)
我能理解它说的是什么,我只是不明白为什么第一个可以,第二个不行!
编辑:啊,我当然也有
perms [] = [[]]
在顶部。
谢谢
最佳答案
在第一个表达式中,您有 x:y
,这意味着如果 x::a
则 y::[a]
。
在 x : perms y
中,如果 x::a
那么它一定是 perms y::[a]
,但是 perms y::[[a]]
(排列列表)。
类型检查器尝试统一 [a]
和 [[a]]
但失败。
关于haskell - Haskell 中的 "Cannot create infinite type"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4437308/