「無法創建無限型」 的錯誤,我想知道爲什麼哈斯克爾接受這個在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的1]編譯主(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,i)< - zip xs [0 ..]'而不是'i < - [0 ..(length xs - 1)],x < - [xs !! i]'好得多 – 2010-12-14 09:01:57
是的!謝謝! – 2010-12-15 01:43:41