Haskell交换列表元素时出错

时间:2013-10-11 23:34:34

标签: list haskell

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

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

1 个答案:

答案 0 :(得分:3)

check期望将一个元组作为其第一个参数;因此,所有对它的调用 - 无论是main还是check本身 - 都必须将它传递给元组。