F # Tutorials-arrays

Source: Internet
Author: User

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 &lt;-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".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.