Celebrating F# – Part 2 (F# Arrays)

In the previous post, we kick started programming with F#. In this post, we will explore more into the programming language.

In F#, Arrays are fixed size, mutable sequence of elements of same type.

An Array is declared within [| and |] symbols. The different elements in an array are separated by a Semi-colon (;). An element can be accessed using “.” operator, (like .[0]).

Each element of an array can be declared in each line in which case, the element separator (;) is not needed.

A range of elements can also be declared.

Steps can be specified to increment the range.

Steps can be negative

All the elements of an array should be of same type, else error is thrown. The type of an array is inferred from its first value.

A sequence expression (like a for statement) can also be used to create an array. The value of each element is retrieved from the Expression on the Right side of “for” statement.

In this example, a for loop is used to create elements of an array.  The expression computes the Area of all Circles with radius 1 to 10 and results in an array.

Notice the “Explicit conversion” of integer to float data type.

To create an array and initialize the elements to 0, Array.ZeroCreate method can be used. The values can then be set to each element in the array, using the Destructive assignment operator (<-). We have used this operator earlier to assign value to mutable variables. In this example, we have explicitly, defined the array to be of “float” type.

A slice of an array can be accessed.

An array can contain another array and is called a Rectangular array or Multidimensional array.

The value of a Rectangular array can be accessed using multiple indexers.

F# Framework brings predefined types like Array2D, Array3D and Array4D to help us to work with multidimensional arrays. Each have methods like zeroCreate to initialize and the values can be set using multiple indexers.

Jagged arrays (Array of arrays of varying lengths) can also be declared.

Arrays can also be created using the “Create” function. Here all the elements are initialized to a value of 1.

We can also use the “Init” method to create an array and initialize values. In fact, the init function can assign different values into each element.

This example creates an array of “Perfect squares”

The length of an array can be accessed using the “Array.Length” property.

An array can be iterated using an iter function.

To use the index in the function, use iteri function. (Note that printf outputs values where printfn creates a new line after value)

An array can be created from an existing array, by applying a transformation using Array.map function. Here is an example that doubles the values of an array and creates a new array.

An array can also be created by transforming 2 arrays using the “Map2” function. The following example shows an Additive transformation of 2 arrays.

Arrays can also be copied using “Array.Copy” function.

A new array can be created by appending values to an existing array using “Array.Append” function.

Cumulative transformation can be performed using the arrays, by the use of Accumulator which accumulates the values. This operation is called as Folding, which can happen forwards or backwards using the fold and foldback functions. The following example adds all the values of an array.

An array can also be created from a sub range of an array, using “Array.sub” function by specifying the start index and length.

Here is a list of other Array functions

  1. choose  – selects the elements of an array to include in a new array
  2. collect – runs a function on all elements and collects the result into new array
  3. concat – combines a set of arrays into one array
  4. filter – filters an array based on condition and creates a new array
  5. rev – reverses the order of an array
  6. exist, exists2 – checks the presence of element in an array
  7. forall, forall2 – validates a condition for all elements
  8. find, findindex, – searches an element that satisfies a condition
  9. tryfind, tryPick,tryFindIndex, – finds and returns the status
  10. average, max, min, sum, sumBy – Mathematical functions
  11. set, fill – sets the values
  12. reduce, reduceBack,scan, scanBack, – execute algorithms
  13. blit, – copy subsection of an array to new array
  14. ofList, ofSeq, – creates from other collections
  15. sortBy, sortWith, sortInPlace, sortInPlaceBy, sortInPlaceWith – Sorting functions
  16. zip, unzip – converts arrays to tuples and vice versa
  17. Parallel – Parallel computation of Arrays

References

  1. http://mariusbancila.ro/blog/2008/03/26/arrays-in-f/
  2. http://www.ctocorner.com/fsharp/book/ch7.aspx
  3. http://strangelights.com/fsharp/wiki/default.aspx/FSharpWiki/ListsAndArrays.html
  4. http://msdn.microsoft.com/en-us/library/dd233214(VS.100).aspx