Determinate (Haskell)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Python

[edit] Recursion

<<matrix_determinate_recursive.hs>>=
determinate :: (Fractional a) => [[a]] -> a
determinate [] = 0
determinate mss = if (numberOfRows== 2) && (numberOfColumns == 2) 
                then (a * d) - (b * c)
                else sum (map determinate submatrices)
                     where numberOfRows = length mss;
                           numberOfColumns = length (mss !! 0);
                           a = mget mss 0 0;
                           b = mget mss 0 1;
                           c = mget mss 1 0;
                           d = mget mss 1 1;
                           submatrices = [ map (tail (remove i mss)) | 
                               i <- [0..length mss -1] ]

Download code
hijacker
hijacker
hijacker
hijacker