Matrix multiplication: Difference between revisions

Content added Content deleted
(→‎Haskell :: With List and without transpose: Fixed compile failure by adding final closing bracket. Applied Ormolu.)
(→‎With List and without transpose - shorter: Preferred guards to `if then`. Applied Ormolu.)
Line 3,268: Line 3,268:
<lang Haskell>mult :: Num a => [[a]] -> [[a]] -> [[a]]
<lang Haskell>mult :: Num a => [[a]] -> [[a]] -> [[a]]
mult uss vss =
mult uss vss =
let go xs
map
((\xs ->
| null xs = []
if null xs
| otherwise = foldl1 (zipWith (+)) xs
then []
in go . zipWith (flip (map . (*))) vss <$> uss
else foldl1 (zipWith (+)) xs) .
zipWith (flip (map . (*))) vss)
uss


main :: IO ()
main :: IO ()
main =
main = mapM_ print $ mult [[1, 2], [3, 4]] [[-3, -8, 3], [-2, 1, 4]]</lang>
mapM_ print $
mult [[1, 2], [3, 4]] [[-3, -8, 3], [-2, 1, 4]]</lang>
{{out}}
{{out}}
<pre>[-7,-6,11]
<pre>[-7,-6,11]