Skip to content

Instantly share code, notes, and snippets.

Created April 19, 2015 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/0f1f67c64fd10043b4bb to your computer and use it in GitHub Desktop.
Save anonymous/0f1f67c64fd10043b4bb to your computer and use it in GitHub Desktop.
The learning process
-- Kata http://www.codewars.com/kata/551204b7509063d9ba000b45/haskell
mainDiagonalProduct :: Num a => [[a]] -> a
-- My implementation
mainDiagonalProduct [] = 1
mainDiagonalProduct mat =
mainDiagonalProduct' mat ((length mat) - 1)
where
mainDiagonalProduct' mat 0 = (head . head) mat
mainDiagonalProduct' mat index =
((mat !! index) !! index) * (mainDiagonalProduct' mat (index - 1))
-- Good implementation
mainDiagonalProduct mat = product $ zipWith (!!) mat [1..]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment