实现转置 - 编译时错误

时间:2014-04-28 02:16:38

标签: haskell transpose

我正在从Learn You a Haskell开始transpose

-- transpose - rows becomes columns and vice-versa
-- transpose [[1,2,3],[4,5,6],[7,8,9]]  
-- [[1,4,7],[2,5,8],[3,6,9]]

这是我的尝试:

transpose' :: [[a]] -> [[a]]
transpose' [[]] = [[]]
transpose' ys = [map head' ys] ++ transpose' (map tail' ys)
         where head' []     = []
               head' (z:zs) = z
               tail' []     = []
               tail' (z:zs) = zs

但是,我不理解与map head' ys调用相关的编译时错误。

> :l Transpose.hs
[1 of 1] Compiling Main             ( Transpose.hs, interpreted )

Transpose.hs:5:22:
    Couldn't match type `a' with `[a0]'
      `a' is a rigid type variable bound by
          the type signature for transpose' :: [[a]] -> [[a]]
          at Transpose.hs:3:15
    Expected type: [[a0]] -> a
      Actual type: [[a0]] -> [a0]
    In the first argument of `map', namely head'
    In the expression: map head' ys
    In the first argument of `(++)', namely `[map head' ys]'
Failed, modules loaded: none.

根据编译时警告,您能否向我解释一下我的代码有什么问题?

0 个答案:

没有答案