Sum of elements below main diagonal of matrix: Difference between revisions
Sum of elements below main diagonal of matrix (view source)
Revision as of 01:25, 24 July 2021
, 2 years ago→{{header|Haskell}}: Generalised the function to define either upper or lower triangles.
m (→{{header|C++}}: Fix condition) |
m (→{{header|Haskell}}: Generalised the function to define either upper or lower triangles.) |
||
Line 189:
=={{header|Haskell}}==
Defining the upper or lower triangle of a square matrix:
<lang haskell>
| upper = go drop id
| otherwise
where
foldr▼
go f g
(\xs (n, rows) -> (pred n, take n xs : rows))▼
|
(g $ length matrix, [])
matrix
| otherwise = Left "Not a square matrix."
isSquare :: [[a]] -> Bool
isSquare rows = all ((n ==) . length) rows
where
Line 215 ⟶ 218:
putStrLn
(print . sum . concat)
( matrixTriangle
[ [1, 3, 7, 8, 10],
[2, 4, 16, 14, 4],
[
[
{{Out}}
<pre>69</pre>
|