无法弄清楚为什么模式匹配不起作用!我从 Haskell 开始,所以请耐心等待!

-- matrix implemented as a list of lists (rows of the matrix)
test_matrix3 = [[1,0,0],[2,-3,0],[4,5,6]]

-- transpose of a given matrix
transpose    (x:[]) = [x]
transpose all@(x:_) = map head all : transpose ([tail y | y <- all])

执行:
*Main> transpose test_matrix3
[[1,2,4],[0,-3,5],[0,0,6],[*** Exception: Prelude.head: empty list

最佳答案

这个对我有用:

transpose' ([]:_) = []
transpose' xs = (map head xs) : (transpose' (map tail xs))

测试:
*Main> transpose' test_matrix3
[[1,2,4],[0,-3,5],[0,0,6]]

关于list - 为什么我得到 "Exception: Prelude.head: empty list"?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4883169/

10-10 06:16