Arrays in F#
Our first example demonstrates the use of arrays in F#. In F#, an array is a fixed-size, zero-based, mutable collection of elements of the same type.
When you run this program, you should see output similar to this:
Note that arrays in F# are displayed in the form [|v1; v2; v3; ...|]
when printed.
F# arrays are similar to those in other languages, but with some key differences:
- F# uses
[| |]
syntax for array literals instead of { }
. - Array indices are accessed using
.
notation (e.g., array.[index]
) instead of []
. - F# doesn’t have a built-in way to create arrays with “holes” like Go’s
[...]int{1, 3: 4}
syntax. - Multi-dimensional arrays in F# use
,
to separate dimensions (e.g., array2D.[i, j]
).
F# also provides many useful functions for working with arrays in the Array
module, which can make array manipulation more concise and functional.