无法在我的Haskell代码中找到无限循环

时间:2016-05-05 21:16:43

标签: haskell

我自己学习Haskell,并且我必须计算一个矩阵的行列式。我刷新了数学方法,并在代码中模拟它,但由于某种原因,它无限循环。我已经在GHCi中输入了几个小时的东西,试图找出导致它的原因,但是我无法找到它。

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

1 个答案:

答案 0 :(得分:1)

dethelper vec mat n+1无法按照您的想法行事。它被解析为(dethelper vec mat n)+1。我想你真的想要dethelper vec mat (n+1)