1

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

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 仍无法匹配。有什么想法吗?

4

1 回答 1

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]
于 2013-05-17T10:01:04.040 回答