transpose' :: [[a]] -> [[a]]
transpose' [[]] = []
transpose' [[], _] = []
transpose' rows = (map head' rows) : transpose' (map tail' rows)
where
head' (x:_) = x
tail' (_:xs) = xs
matMult3 :: (Num a) => [[a]] -> [[a]] -> [[a]]
matMult3 a b = [ [ sum $ zipWith (*) ar bc | bc <- (transpose' b) ] | ar <- a ]
任何人都有任何想法,为什么这可能是一个非详尽的?我觉得有些东西不见了,但我太新了,不能让自己真正知道,我自我教导这可能就是为什么,任何建议都会受到赞赏。
答案 0 :(得分:2)
如果您将空列表作为参数提供,则head'
和tail'
都不会定义会发生什么。您需要定义以下内容:
head' [] = ....
tail' [] = ....
答案 1 :(得分:1)
我在Haskell中的表达不是很流畅,但是transpose'
的以下版本适用于非粗糙矩阵:
transpose' :: [[a]] -> [[a]]
transpose' ([x]: xs) = [x: map singleTail' xs] where
singleTail' [x] = x
transpose' rows = (map head rows) : transpose' (map tail rows)
,例如,
>>> transpose' [[1], [2], [3]]
[[1,2,3]]
>>> transpose' [[1,2,3]]
[[1], [2], [3]]
>>> transpose' [[1, 2], [2, 3], [3, 4]]
[[1,2,3],[2,3,4]]
但是参差不齐的矩阵失败了:
>>> transpose' [[1, 2], [2, 3], [3, 4, 5]]
:3:5-23: Non-exhaustive patterns in function singleTail'