I have written a code in Haskell that calculates the determinant of a matrix using the laplace expansion. As I have just started learning Haskell, I would like some opinions on how well (structured) the code is written and where I could be able to boost the performance.
This is the code:
det :: (Num a, Ord a) => [[a]] -> a
det [[]] = error "Cannot calculate Det from [[]]"
det x
| lfex /= lx = error "Cannot calculate Det from non square matrix"
| lfex == 2 && (length s) == 2 = a*d - b*c
| otherwise = foldl (\acc y -> let ind = index y in
acc + (sign ind) * f!!ind * (det $ genLaplaceMatrix x ind)) 0 [0 .. lx-1]
where
[[a,b],[c,d]] = x
f:s:xr = x
lx = length x
lfex = length f
sign y = (-1)^(1+lx-y)
index y = lx-1-y
--pivotrow = 1
--pivotcol 0 based!
genLaplaceMatrix :: (Num a) => [[a]] -> Int -> [[a]]
genLaplaceMatrix (_:x) pivotCol = foldr (\y -> (conVecs y:)) [] [0..((length x)-1)]
where
parts vec = splitAt pivotCol vec
conVecs row = let y = x!!row in (fst $ parts y) ++ (tail $ snd $ parts y)
I already looked at this question and tried to implement it as good as possible. Do you have any further advice?