Haskell中的函数守卫和'where'语法

3

我正在尝试学习 Haskell,我决定通过编写一个简单的函数来练习反转 3x3 矩阵。这应该很容易,但是我尝试的所有内容都无法成功编译。

这是我的代码:

matInv3x3 :: [[Double]] -> [[Double]]
matInv3x3 m
    | length m /= 3         = error "wrong number of rows"
    | length (m !! 0) /= 3  = error "wrong number of elements in row 0"
    | length (m !! 1) /= 3  = error "wrong number of elements in row 1"
    | length (m !! 2) /= 3  = error "wrong number of elements in row 2"
    | det == 0              = error "zero determinant"
    | otherwise = mInv
    where   a = m !! 0 !! 0
            b = m !! 0 !! 1
            c = m !! 0 !! 2
            d = m !! 1 !! 0
            e = m !! 1 !! 1
            f = m !! 1 !! 2
            g = m !! 2 !! 0
            h = m !! 2 !! 1
            i = m !! 2 !! 2
            det = a*(e*i - f*h) - b*(i*d - f*g) + c*(d*h - e*g)
            A = (e*i - f*h) / det
            B = -(d*i - f*g) / det
            C = (d*h - e*g) / det
            D = -(b*i - c*h) / det
            E = (a*i - c*g) / det
            F = -(a*h - b*g) / det
            G = (b*f - c*e) / det
            H = -(a*f - c*d) / det
            I = (a*e - b*d) / det
            mInv = [[A,B,C],[D,E,F],[G,H,I]]

我正在尝试防范一切可能出错的情况:不良的列表维度和零行列式。我参考了“学习您的...”书中的示例。如果矩阵具有零行列式,我正在尝试依赖惰性求值。
GHCi无法编译它,在第10行(定义b的位置)上引用了一个解析错误。我确定我缺少了一些简单而基本的东西。有人能指出我做错了什么吗?
更新:
我实现了评论中提出的修复方法,并纠正了我之前犯下的索引交换错误(之前没有注意到,因为代码无法编译)。这是已修复的代码,可以正确地反转3x3矩阵:
matInv3x3 :: [[Double]] -> [[Double]]
matInv3x3 m
    | length m /= 3         = error "wrong number of rows"
    | length (m !! 0) /= 3  = error "wrong number of elements in row 0"
    | length (m !! 1) /= 3  = error "wrong number of elements in row 1"
    | length (m !! 2) /= 3  = error "wrong number of elements in row 2"
    | abs det < 1.0e-15     = error "zero or near-zero determinant"
    | otherwise = mInv
    where   [[a,d,g],[b,e,h],[c,f,i]] = m
            det = a*(e*i - f*h) - b*(i*d - f*g) + c*(d*h - e*g)
            a' = (e*i - f*h) / det
            b' = -(d*i - f*g) / det
            c' = (d*h - e*g) / det
            d' = -(b*i - c*h) / det
            e' = (a*i - c*g) / det
            f' = -(a*h - b*g) / det
            g' = (b*f - c*e) / det
            h' = -(a*f - c*d) / det
            i' = (a*e - b*d) / det
            mInv = [[a',b',c'],[d',e',f'],[g',h',i']]

当我编译它时,你提到的那一行没有错误——也许你编辑器中的缩进与这里不同?另外,你不能给变量命名以大写字母开头——AB..I都是无效的。你可以使用_A - user2407038
2
这些值的传统命名应该是 a'i'。 GHC 会特别处理以 _ 开头的值 - 如果它们未使用,它不会发出警告。我不建议使用这种命名方式。 - Carl
啊,好的了解。我最好早点学习好的习惯。 - user2790167
4
дҪ еҸҜд»ҘдҪҝз”ЁеҚ•иЎҢд»Јз Ғ[[a,d,g],[b,e,h],[c,f,i]] = mжқҘдёәзҹ©йҳөзҡ„жүҖжңүе…ғзҙ иҝӣиЎҢиөӢеҖјгҖӮ - max taldykin
1
@user2790167 你能否把你的回答放在回答框中,而不是放在问题里呢? - not my job
显示剩余7条评论
1个回答

1
一个好的练习是将这个函数推广到任意nxn矩阵。以下是一种计算nxn行列式的方法,如果您感兴趣可以作为起点。
-- Remove the nth element from a list
remove :: Int -> [a] -> [a]
remove n xs = ys ++ (tail zs)
  where
    (ys, zs) = splitAt n xs

-- Minor matrix of cofactor C(i,j)
minor :: Int -> Int -> [[a]] -> [[a]]
minor i j xs = remove j $ map (remove i) xs

-- The determinant of a square matrix represented as a list of lists
-- representing column vectors, that is [column].
det :: Num a => [[a]] -> a
det (a:[]) = head a
det m = sum [(-1)^i * (c1 !! i) * det (minor i 0 m) | i <- [0 .. (n-1)]]
  where
    c1 = head m
    n = length m

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接