type Matrix = [[Int]] type Column = [Int] type Row = [Int] extractColumn :: Matrix -> Int -> Column extractColumn m n = map (\row -> row!!(n-1)) m removeColumn :: Matrix -> Int -> Matrix removeColumn m n = map (\row -> take (n-1) row ++ drop n row) m transpose :: Matrix -> Matrix transpose [] = [] -- in case input matrix is empty transpose ([]:_) = [] -- base case transpose (x:xs) = extractColumn (x:xs) 1 : transpose (removeColumn (x:xs) 1) scalarProduct :: Row -> Row -> Int scalarProduct xs ys = sum (map (\pair -> fst pair * snd pair) (zip xs ys)) rowMul :: Row -> Matrix -> Row rowMul r = map (`scalarProduct` r) mmul :: Matrix -> Matrix -> Matrix mmul m n = map (`rowMul` n) m matrixMultiply :: Matrix -> Matrix -> Matrix matrixMultiply m n = m `mmul` transpose n determinant :: Matrix -> Int determinant [] = 0 determinant [[x]] = x determinant (row:rest) = dhelper row rest 1 1 0 dhelper :: Row -> Matrix -> Int -> Int -> Int -> Int dhelper row rest col sign answer | col > length row = answer | otherwise = dhelper row rest (col+1) (-sign) answer + sign * (row!!(col-1)) * determinant (removeColumn rest col) --m1 = [[1,2,3],[4,5,6]] --extractColumn m1 1 --extractColumn m1 2 --extractColumn m1 3 --removeColumn m1 1 --removeColumn m1 2 --removeColumn m1 3 --transpose m1 --transpose [] --scalarProduct [1,2] [5,6] --m2 = [[1,2],[3,4]] --m3 = [[5,6],[7,8]] --m4 = [[2,3,4],[5,6,7]] --m5 = [[1,2,3,4],[2,3,5,6],[7,1,2,4]] --m6 = [[4,6],[3,8]] --m7 = [[99]] --m8 = [] --m9 = [[6,1,1],[4,-2,5],[2,8,7]] --matrixMultiply m2 m3 --matrixMultiply m4 m5 --determinant m6 --determinant m7 --determinant m8 --determinant m9