余因子展開による行列式の計算をする関数det。
matrix.hs
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