dev*_*ium 2 haskell functional-programming
我想知道为什么Haskell会接受这个
perms xs = [ x:y | i <- [0..(length xs - 1)], x <- [xs!!i], y <- perms (takeOut i xs)]
Run Code Online (Sandbox Code Playgroud)
但不会接受:
perms xs = [ x:(perms y) | i <- [0..(length xs - 1)], x <- [xs!!i], y <- (takeOut i xs)]
Run Code Online (Sandbox Code Playgroud)
它抱怨说
[1/1]编译Main(abc.hs,解释)
Run Code Online (Sandbox Code Playgroud)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 [] = [[]]
Run Code Online (Sandbox Code Playgroud)
在顶部.
谢谢
在第一个表达式中你x:y有意思,如果x :: a那么y :: [a].在x : perms y如果x :: a那么它必须是perms y :: [a],但perms y :: [[a]](排列的列表).Typechecker试图统一[a]和[[a]]和失败.