Skip to content

Instantly share code, notes, and snippets.

@pawelrubin
Last active December 22, 2020 18:17
Show Gist options
  • Save pawelrubin/62e6d73dcbfe1aa2c86f8e096efc3322 to your computer and use it in GitHub Desktop.
Save pawelrubin/62e6d73dcbfe1aa2c86f8e096efc3322 to your computer and use it in GitHub Desktop.
Calculate the absolute difference between the sums of a square matrix diagonals, in Haskell.
diagonalDifference :: [[Int]] -> Int
diagonalDifference arr = abs . sum $ differences
where
differences = map (\(i, row) -> (row !! i) - (row !! (len - 1 - i))) (enumerate arr)
len = length arr
enumerate = zip [0 ..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment