Concept of Arrays in F#


Introduction

Today I am discussing Arrays. Most of us are familiar with the concept of arrays as they work in almost all programming language. Arrays are a mutable collection type in F#. Here mutable means the values in an array can be changed after the array has been created. Arrays are ubiquitous; a familiar Data Structure used to represent a group of related, ordered values. An array contain a zero based, fixed size mutable sequence of consecutive data elements, all of the same type.

Arrays and Lists are both collections but an array has a number of properties which makes Arrays different from Lists. You can update the values which are in an Array but in a List you can not update. On the other hand a List can grow dynamically where as Arrays can not grow. Different Arrays called by different names like one dimensional arrays are referred as Vector and multidimensional are matrices.

You can define Arrays by Sequence of Items which can be separated by Semicolons. When you create an Array this will start by an opening square bracket and a Vertical bar([|) and end with a vertical bar and a Closing Square Bracket(|]).
Create And Access Array

Arrays can be created in one of the several ways. The general syntax for an array creation is as follows.

let ar=[| element1;element2;....... or array comprehension |]  //syntax for creating Array

You can also put array items in separate lines; in that case the semicolon is optional.

// Syntax for creating Arrays in separate lines
let ar=
[|
    element1
    element2
    .......
    elementn
|]

Defining an Array literal

You can define an Array literal in the same way as lists by placing the array elements between this notation [||] like below.

//Defining an Array
let num=[|
    "one";
    "two";
    "three";
    "four"
    |]

How to Reference an Array

The syntax for referencing an Array element is the name of the identifier of the array followed by a dot (.) known as a period and then the index of the element in Square brackets ([]) like below.

// syntax for referencing Array
let firstNum = num.[0]
let secondNum = num.[1]
let thirdNum = num.[2]
let fourthNum = num.[3]

How to set the value of an Array Element

You can set the value of an array element by left arrow and the value you want to assign followed by the left arrow.

//Syntax for setting Values to array elements
num.[0] <- " abc,"
num.[1] <- " def,"
num.[2] <- " ghi,"
num.[3] <-
" jkl "

Example-

Array


//example of array
let city = [|
    "Delhi"
    "Noida"
    "Gaziyabad" 
    "Hapur"
|]
printfn "%s" city.[0]  //Delhi  
printfn "%s" city.[1]  //Noida
printfn "%s" city.[2]  //Gaziyabad
printfn "%s" city.[3] 
//Hapur

Output-

array

Multidimensional Array

A multidimensional array can be defined as an Array of an Array. In F# a multidimensional array is defined in two categories generally referred to as a Jagged Array and a Rectangular array.


  • Jagged Array

A Jagged Array is referred to as an Array of an Array where the inner array is of a varying size, when written out showing an entire array in "jagged" appearance. You can also say that the second dimension is not in a regular shape.

Example-

Jagged Array

//example of jagged array
let num = [|[|1; 2; 3|]; [|4; 5; 6|]|]
let row = num.[0]    // [|4; 5; 6|]
let elementone = row.[0]  // 4
let elementtwo = num.[1].[0] // 4 – .operator used two times
printfn "%i %i" elementone elementtwo

Output-

Jagged Array


  • Rectangular Array

A rectangular array is an array which contains one or more other arrays and all inner arrays are of the same length. The resulting data structure is like a matrix or grid type.

Example-

Rectangular Array


//example of Rectangular array
// create a square array,
// initally start with zeros
let sqr = Array2D.create 2 2 0
// populate the array
sqr.[0,0] <- 1
sqr.[0,1] <- 2
sqr.[1,0] <- 3
sqr.[1,1] <- 4
// print the array
printfn "%A" sqr

Output-

Rectangular Array



Similar Articles