数组的定义:
[<EntryPoint>] let main argv = let iArr1 = [|1; 3; 5; 7; 9|] iArr1 |> printfn "%A" let eArr = [| |] eArr |> printfn "%A" let iArr2 = [|1..5|] iArr2 |> printfn "%A" let iArr3 = [|1..2..10|] iArr3 |> printfn "%A" let iArr4 = [|for i in 0..4 -> 2*i+1 |] iArr4 |> printfn "%A" let fArr1 = [|for i in 10..-2..0 -> 2.0 * (float i) - 0.5|] fArr1 |> printfn "%A" let fArr2 = [|for i in iArr4 -> (float i) / 2.0 |] fArr2 |> printfn "%A" let x = fArr1.[1] fArr1.[1] <- 7.2 fArr1.[2] <- fArr1.[2] * 2.0 let tpArr1 = [| for x in 3..9 -> (x,x*x) |] tpArr1 |> printfn "%A" (iArr1 = iArr2) |> printfn "%b" (iArr1 = iArr3) |> printfn "%b" 0 // 返回整数退出代码
F#数组定义支持函数式定义,如for表达式等,支持直接比较。当元素的长度与值都相等时则相等,不同数组类型之间是不能比较的。
数组常用函数:
[<EntryPoint>] let main argv = let arr1 = [|2;4;8|] let l1 = arr1.Length let arr2 = [|1;3;5;7;9|] arr1.CopyTo(arr2,2) arr2 |> printfn "%A" let arr4 = [|0.5..7.5|] arr1.CopyTo(arr4,0) let arr5 = Array.create 6 2 let arr6 = [|1..10|] let arr7 = Array.sub arr6 0 4 arr7 |> printfn "%A" arr6 |> printfn "%A" let arr8 = Array.append arr5 arr7 arr8 |> printfn "%A" let arr9 = [|"北京";"上海";"天津";"重庆"|] let arr10 = Array.zip arr7 arr9 arr10 |> printfn "%A" let arr11,arr12 = Array.unzip arr10 arr11 |> printfn "%A" arr12 |> printfn "%A" let arr13 = [|1.6;1.9;1.0;0.7|] let arr14 = Array.zip3 arr7 arr9 arr13 arr14 |> printfn "%A" let _,arr15,_ = Array.unzip3 arr14 arr15 |> printfn "%A" let fArr3 = [|6.5;1.2;9.8;4.0;7.35;2.4;7.3;9.5|] let x1 = Array.average fArr3 fArr3 |> printfn "%A" let x2 = Array.sum fArr3 x2 |> printfn "%f" let fArr4 = Array.sort fArr3 fArr4 |> printfn "%A" fArr3 |> printfn "%A" // sort 函数不改变数组本身 Array.sortInPlace fArr3 // sortInPlace 函数对数组本身进行排序处理 0 // 返回整数退出代码
其实和scala没什么太大的区别,标准函数都支持。
二维数组:
[<EntryPoint>] let main argv = let mArr1 = Array2D.create 3 4 1 mArr1 |> printfn "%A" mArr1.[2,1] <- 5 mArr1.[2,2] <- mArr1.[1,1] + mArr1.[2,1] mArr1.[2,3] <- 7 printfn " Length1=%i, length2=%i" (mArr1.GetLength 0) (mArr1.GetLength 1) mArr1 |> printfn "%A" let msum (arr : int[,]) = let sums = Array.create (arr.GetLength 0) 0 for i = 0 to (arr.GetLength 0) - 1 do for j = 0 to (arr.GetLength 1) - 1 do sums.[i] <- sums.[i] + arr.[i,j] sums (msum mArr1) |> printfn "%A" 0 // 返回整数退出代码
使用Array2D库创建2维数组msum将每个维度的值求和,最后输出
三维,四维数组:
let mArr2 = Array3D.create 3 2 3 0.0 let mArr3 = Array4D.create 2 2 2 3 true mArr2.[0,0,0] <- 0.27 mArr3.[1,1,1,1] <- false
不规则多维数组:
[<EntryPoint>] let main argv = let mArr4 = [|[|1;1|]; [|1;2;1|]; [|1;3;3;1|]|] mArr4 |> printfn "%A" mArr4.[0] |> printfn "%A" let a = mArr4.[1] let b,c = a.[0],mArr4.[2].[2] printfn "Length=%i GetLength=%i" mArr4.Length (mArr4.GetLength 0) for i=0 to mArr4.Length-1 do printf "[%i].Length=%i " i mArr4.[i].Length let arr1 = [|1..4|] let arr2 = [|1..2..10|] let mArr5 = [|arr1;arr2;[|for i in arr1 -> 1+i|];[|for i in arr2 -> 2*i|]|] mArr5 |> printfn "%A" 0 // 返回整数退出代码
杨辉三角形:
[<EntryPoint>] let main argv = let yanghui n = let arr = Array.create n [|1|] printfn "%A" arr for i = 1 to n-1 do arr.[i] <- (Array.create (i+1) 0) arr.[i].[0] <- arr.[i-1].[0] for j = 1 to i-1 do arr.[i].[j] <- arr.[i-1].[j-1] + arr.[i-1].[j] arr.[i].[i] <- arr.[i-1].[i-1] arr let arr = 10 |> yanghui for i in 0..arr.Length-1 do printfn "%A" arr.[i] 0 // 返回整数退出代码
时间: 2024-10-18 12:16:19