0

I'm learning myself Haskell, and for one exercise I have to calculate the determinant for a Matrix. I refreshed the mathematical way to do it and emulated it in code, but for some reason it's looping infinitely. I've been entering stuff into GHCi for hours trying to pinpoint what's causing it but I can't find it.

type Vector = [Float]
type Matrix = [Vector]

determinant :: Matrix -> Float
determinant [] = 0
determinant [[a,b],[c,d]] = (a*d) - (b*c)
determinant (vec:mat) = dethelper vec mat 0

dethelper :: Vector -> Matrix -> Int -> Float
dethelper vec mat n
  | vec == []              = 0
  | n == length vec        = 0 
  | even n                 = (vec!!n * (determinant $ map (dropAt n) mat)) - (dethelper vec mat n+1)
  | otherwise              = (vec!!n * (determinant $ map (dropAt n) mat)) + (dethelper vec mat n+1)

dropAt :: Int -> Vector -> Vector
dropAt x xs = (fst spl) ++ (tail $ snd spl)
               where spl = splitAt x xs
3
  • 2
    How do you invoke it? Have you tested each one of the functions? Commented May 5, 2016 at 21:18
  • 2
    dethelper vec mat n+1 means (dethelper vec mat n)+1 not dethelper vec mat (n+1), which is presumably what you wanted. Commented May 5, 2016 at 21:52
  • @user2407038 yup, that was it. stupid mistake , thanks! Commented May 5, 2016 at 22:44

1 Answer 1

1

dethelper vec mat n+1 does not work how you think it does. It is parsed like (dethelper vec mat n)+1. I think you actually want dethelper vec mat (n+1)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.