This time we learn the array.
let array = [|1;2;3;4;5|]
printfn "%A" array
for n in array do
printfn "%d" n
printfn "%d" array.[0]
printfn "%d" array.[1]
printfn "%d" array.[2]
printfn "%d" array.[3]
printfn "%d" array.[4]
Array with [| ... |] Said. The For statement here is the same as the C # foreach.
Instead of using array[0 to access each of the arrays, you use array. [0]. I'm not used to it!
The output results are:
[|1; 2; 3; 4; 5|]
1
2
3
4
5
1
2
3
4
5
To change the element, you can use the <-operator.
for j = 0 to 4 do
array1.[j] <- array1.[j] * 2
for n in array1 do
printfn "%A" n
For arrays that cannot specify mutable, the element values can be changed by the <-operator.
Finally, array initialization can be done using the following code:
Let arr = array.create 4 "Hello"
This allows you to generate an array of 4 elements and initialize each element to "hello".