Haskell-类型匹配

时间:2013-04-17 22:14:38

标签: haskell types matching

我无法从辅助函数获取输出以匹配我使用以下代码的函数输出:

getSemiDiag :: [[Maybe Player]] -> Int -> Int -> [Maybe Player]
getSemiDiag [] _ _ = []
getSemiDiag (x:xs) start size = if start > (size -1)
                            then []
                            else (x !! start) : (getSemiDiag xs (start+1) size)



semiRight :: [[Maybe Player]] -> Int -> Int -> [[Maybe Player]]
semiRight [] _ _ = []
semiRight (x:xs) start size = if start > (size -1)
                          then []
                          else (getSemiDiag x start size) : (semiRight xs (start+1) size)
尽管我付出了最大努力,但函数semiRight仍然无法匹配。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

仅从类型判断,x与类型为[Maybe Player]的元素匹配,

你显然应该将[x]而不是x传递给getSemiDiag。 而且,一些括号是多余的。

semiRight :: [[Maybe Player]] -> Int -> Int -> [[Maybe Player]]
semiRight [] _ _ = []
semiRight (x:xs) start size = if start > (size -1)
                          then []
                          else getSemiDiag [x] start size : semiRight xs (start+1) size

为什么不使用zipWith

semiRight players start size = zipWith (\p i -> [p !! i]) players [start .. size]