Skip to content

Instantly share code, notes, and snippets.

@nnabeyang
Created January 20, 2014 00:26
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 nnabeyang/8512915 to your computer and use it in GitHub Desktop.
Save nnabeyang/8512915 to your computer and use it in GitHub Desktop.
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = [x| (x, j) <- zip xs [0..], i /= j]
cofactor :: Int -> Int -> Matrix -> Matrix
cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
det :: Matrix -> Int
type Matrix = [[Int]]
dropAt :: Int -> [a] -> [a]
dropAt i xs = as ++ bs where (as, _ :bs) = splitAt i xs
cofactor :: Int -> Int -> Matrix -> Matrix
cofactor i j m = [dropAt j v| (v, k) <- zip m [0..], i /= k]
det :: Matrix -> Int
det m = case m of
[[e]] -> e
(xs:xss) -> sum [(-1) ^ i * e * (det . cofactor 0 i) m |
(e, i) <- zip xs [0..]]
ex :: Int
ex = det [[1, 2],
[3, 4]]
-- 1*4 - 2 * 3 = 4 - 6 = -2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment