1

有谁知道为什么这仍然给我错误?

main = do
print $ check [4,3,2] 0 1 
-- output expected [3,4,2], means just once check and swap not more

check ( modXs, []) _ _  = modXs
check ( modXs, [x]) _ _ = x : modXs
check ( modXs, (x1:x2:xs)) counter limit 
    | x1 > x2 && counter < limit =  x2:check (x1 : xs)  (counter+1) limit
    | otherwise = x1 : check (x2 : xs) counter limit 

这里的错误消息说明了我什至不明白的类型:

Couldn't match expected type `([a1], [a1])' with actual type `[a1]'
    In the first argument of `check', namely `(x1 : xs)'
    In the second argument of `(:)', namely
      `check (x1 : xs) (counter + 1) limit'
    In the expression: x2 : check (x1 : xs) (counter + 1) limit
4

1 回答 1

3

check期望收到一个元组作为它的第一个参数;所以所有对它的调用——无论是在自身内部还是maincheck自身内部——都必须传递一个元组。

于 2013-10-12T00:18:45.283 回答