递归计算 N x N 矩阵行列式

Recursively computing an N x N matrix determinant

我对此实施有疑问。它给我一个错误,整个块上的 "this expression to have type bool but here has unit" 从第一个 for 循环开始并以 pown 表达式结束。

我不太清楚这是什么意思。

let rec detMat (m : int[,] ) : int = 
    let mutable det = 0
    let mutable n = m.Length
    let mutable i = 0
    let mutable j = 0
    let mutable j = 0
    let mutable j1 = 0
    let mutable j2 = 0
    let mTmp = Array2D.create 0

    if n  = 1 then
        det = m.[0, 0]
    elif n = 2 then
        det = m.[0, 0] * m.[1, 1] - m.[1, 0] * m.[0, 1]
    else
        det = 0
        for j1 = 0 to n do
            for i = 1 to n do
                j2 = 0
                for j = 0 to n do
                    if j <> j1 then
                        mTmp.[i-1, j2] <- m.[i, j]
                        j2 = j2 + 1
            pown -1 (1 + j1 + 1) * m.[0, j1] * detMat(mTmp, n-1)
    det

<- 是可变变量的赋值运算符。 你的代码应该是

det <- m.[0, 0]

det <- m.[0, 0] * m.[1, 1] - m.[1, 0] * m.[0, 1]

j2 <- j2 + 1

= 是为了平等,这就是为什么您会收到 "this expression to have type bool" 错误。第二部分 "but here has unit" 意味着编译器期望在最后一个表达式中有一个 if 到 return 的单元。仔细阅读此页面以了解错误消息为何如此混乱:https://msdn.microsoft.com/en-us/library/dd233231.aspx