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 に答える