0

How to de/reference the 3 array variables in this code instead of using mutable values?

The code below computes the Longest common subsequence (LCS) by diagonal traversing the m*n array.

The arguments are 2 char arrays like so:

So LCS method should result to length 4 as the longest common sub-sequence chars are "acbb" & "bcbb".

let private s1 = "ABCDBB".ToCharArray()    
let private s2 = "CBACBAABA".ToCharArray()
    let public lcs_seq_1d_diags (x:char[]) (y:char[]) = 
        let m = x.Length
        let n = y.Length

        let mutable dk2 = Array.create (1+m) 0
        //printfn "\r\n0: %A" dk2
        let mutable dk1 = Array.create (1+m) 0
        //printfn "1: %A" dk1
        let mutable dk = Array.create (1+m) 0

        for k = 2 to m+n do
            let low = max 1 (k-m)
            let high = min (k-1) n

            for j = low to high do
                let i = k - j
                if x.[i-1] = y.[j-1] then
                    dk.[i] <- dk2.[i-1] + 1
                else 
                    dk.[i] <- max dk1.[i] dk1.[i-1]

            let mutable temp = dk2
            dk2 <- dk1
            dk1 <- dk
            dk <- temp

        dk1.[m]

let private res_seq_1d_rows = duration (fun () -> lcs_seq_1d_rows s1 s2)
//res_seq_1d_rows = 4
4

2 回答 2

0

默认情况下,数组是可变的。如果您想要不变性,请尝试使用列表。

尝试从这个开始:

let s1 = List.ofSeq "ABCDBB"
let s2 = List.ofSeq "CBACBAABA"

剩下的我留给读者练习:-)

于 2013-04-15T12:23:43.620 回答
0

查看参考单元格http://msdn.microsoft.com/en-us/library/dd233186.aspx

语法如下所示:

let a = ref 1 // declaring a reference
a := 2 // changing the reference value
printfn "%i" !a // dereferencing

这可能也很有趣:F#: let mutable vs. ref

于 2013-04-15T07:41:35.857 回答