Until now, when you’ve needed to join multiple pieces of data together, you’ve used lists. Lists are extremely efficient at adding and removing elements from the beginning of a list, but they aren’t ideal for every situation. For example, random access of list elements is very slow. In addition, if you needed to change the last element of a list, you would need to clone the entire list. (The performance characteristics of lists will be covered more in-depth in Chapter 7.)
Arrays in .NET are a contiguous block of memory containing zero or more elements, each of which can be modified individually (unlike lists, which are immutable).
Arrays can be constructed using array comprehensions, which are
identical to list comprehensions (discussed in Chapter 2), or manually via a list of values separated by semicolons and enclosed between
> // Using the array comprehension syntax let perfectSquares =
[| for i in 1 .. 7 -> i * i |];; val perfectSquares : int array > perfectSquares;; val it : int array = [|1; 4; 9; 16; 25; 36; 49|] > // Manually declared let perfectSquares2 =
[|1; 4; 9; 16; 25; 36; 49; 64; 81
|];; val perfectSquares2 : int array
> // Indexing an array printfn "The first three perfect squares are %d, %d, and %d" perfectSquares
.;; The first three perfect squares are 1, 4, and 9 val it : unit = ()
Example 4-5 uses array indexers to change individual characters of a character array to implement a primitive form of encryption known as ROT13. ROT13 works by simply taking each letter and rotating it 13 places forward in the alphabet. This is achieved in the example by converting each letter to an integer, adding 13, and then converting it back to a character.
Example 4-5. ROT13 encryption in F#
open System // Encrypt a letter using ROT13 let rot13Encrypt (letter : char) = // Move the letter forward 13 places in the alphabet (looping around) // Otherwise ignore. if Char.IsLetter(letter) then let newLetter = (int letter) |> (fun letterIdx -> letterIdx - (int 'A')) |> (fun letterIdx -> (letterIdx + 13) % 26) |> (fun letterIdx -> letterIdx + (int 'A')) |> char newLetter else letter // Loop through each array element, encrypting each letter let encryptText (text : char) = for idx = 0 to text.Length - 1 do
let letter = text.[idx]
text.[idx] <- rot13Encrypt letterlet text = Array.ofSeq "THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG" printfn "Origional = %s" <| new String(text) encryptText(text) printfn "Encrypted = %s" <| new String(text) // A unique trait of ROT13 is that to decrypt, simply encrypt again encryptText(text) printfn "Decrypted = %s" <| new String(text)
The output of our simple program is:
Origional = THE QUICK FOX JUMPED OVER THE LAZY DOG Encrypted = GUR DHVPX SBK WHZCRQ BIRE GUR YNML QBT Decrypted = THE QUICK FOX JUMPED OVER THE LAZY DOG
// Incorrect x // Correct x.
Attempting to access an element in the array with an index either
less than zero or greater than or
equal to the number of elements in the array will raise an
(Exceptions will be covered later in this chapter.) Fortunately, arrays
Length property, which will
return the number of items in the array. Because array indexes are
zero-based, you need to subtract 1 from
Length to get the index of the last element in
> let alphabet = [| 'a' .. 'z' |];; val alphabet : char array > // First letter alphabet.;; val it : char = 'a' > // Last leter alphabet.[alphabet.Length - 1];; val it : char = 'z' > // Some non-existant letter alphabet.;;
System.IndexOutOfRangeException: Index was outside the bounds of the array.at <StartupCode$FSI_0012>.$FSI_0012._main() stopped due to error
When you’re analyzing data stored in arrays, it is sometimes convenient to just work with a subset of the data. In F#, there is a special syntax for taking a slice of an array, where you specify optional lower and upper bounds for the subset of data. The syntax for taking a slice is:
If no lower bound is specified,
0 is used. If no upper bound is specified, the
length of the array
-1 is used. If
neither a lower nor upper bound is specified, you can us an
* to copy the entire
Example 4-6 shows the various ways for taking a slice of an array, but we will break it down line by line shortly.
Example 4-6. Using array slices
open System let daysOfWeek = Enum.GetNames( typeof<DayOfWeek> ) // Standard array slice, elements 2 through 4 daysOfWeek.
[2..4]// Just specify lower bound, elements 4 to the end daysOfWeek.
[4..]// Just specify an upper bound, elements 0 to 2 daysOfWeek.
[..2]// Specify no bounds, get all elements (copies the array) daysOfWeek.
The first way we sliced an array was specifying both an upper and lower bound; this returned all array elements within that range:
> // Standard array slice, elements 2 through 4 daysOfWeek.
[2..4];; val it : string  = [|"Tuesday"; "Wednesday"; "Thursday"|]
Next we specified just a lower or just an upper bound. This returns each element from the lower bound to the end of the array, or from the beginning of the array to the upper bound:
> // Just specify lower bound, elements 4 to the end daysOfWeek.
[4..];; val it : string  = [|"Thursday"; "Friday"; "Saturday"|] > // Just specify an upper bound, elements 0 to 2 daysOfWeek.
[..2];; val it : string  = [|"Sunday"; "Monday"; "Tuesday"|]
And finally we just copied the entire array using
*. Note that for every slice operation a new
array is returned, so there will never be problems with aliasing:
> // Specify no bounds, get all elements (copies the array) daysOfWeek.
Array comprehensions and manually specifying each element
aren’t the only ways to construct arrays. You can also use the
Array.init takes a function used to generate
each array element based on its index. To create an uninitialized array,
With that function, each element is initialized to its default value:
Example 4-7 shows how
Array.zeroCreate to construct arrays.
Example 4-7. Initializing arrays using Array.init
> // Initialize an array of sin-wave elements let divisions = 4.0 let twoPi = 2.0 * Math.PI;; val divisions : float = 4.0 val twoPi : float = 6.283185307 > Array
.init (int divisions) (fun i -> float i * twoPi / divisions);; val it : float array = [|0.0; 1.570796327; 3.141592654; 4.71238898|] > // Construct empty arrays let emptyIntArray : int array =
Array.zeroCreate 3let emptyStringArray : string array =
Array.zeroCreate 3;; val emptyIntArray : int array = [|0; 0; 0|] val emptyStringArray : string array = [|null; null; null|]
Pattern matching against arrays is just as easy as using
lists. And just like pattern matching against lists, when matching
against arrays, you can capture element values, as well as match against
the structure of the array. Example 4-8 shows matching an array
null or an array with
0, 1, or 2 elements.
Example 4-8. Pattern matching against arrays
> // Describe an array let describeArray arr = match arr with |
null-> "The array is null" |
[| |]-> "The array is empty" |
[| x |]-> sprintf "The array has one element, %A" x |
[| x; y |]-> sprintf "The array has two elements, %A and %A" x y | a -> sprintf "The array had %d elements, %A" a.Length a;; val describeArray : 'a array -> string > describeArray [| 1 .. 4 |];; val it : string = "The array had 4 elements, [|1; 2; 3; 4|]" > describeArray [| ("tuple", 1, 2, 3) |];; val it : string = "The array has one element, ("tuple", 1, 2, 3)"
Arrays in F# are compared using structural equality. Two arrays are considered equal if they have the same rank, length, and elements. (Rank is the dimensionality of the array, something we will cover in the next section.)
> [| 1 .. 5 |] = [| 1; 2; 3; 4; 5 |];; val it : bool = true > [| 1 .. 3 |] = [| |];; val it : bool = false
This is different from the behavior of equality on
arrays in C#. In F#, the
contains special logic for comparing arrays so the default referential
equality is not used. For more information on object equality in .NET,
refer to Chapter 5.
Just like the
Seq modules, there is an
Array module detailed in Table 4-1 that contains nearly
identical functions for manipulating arrays.
Table 4-1. Common methods in the Array module
Returns the length of an
array. Arrays already have a
Creates a new array with the given number of elements; each element is initialized by the result of the provided function.
Creates an array with the given length where each entry is the type’s default value.
Tests if any element in the array satisfies the given function.
Array.partition divides the
given array into two new arrays. The first array contains only
elements where the provided function returns
true; the second array contains elements
where the provided function returns
> // Simple Boolean function let isGreaterThanTen x = if x > 10 then true else false;; val isGreaterThanTen : int -> bool > // Partitioning arrays [| 5; 5; 6; 20; 1; 3; 7; 11 |] |>
Array.partition isGreaterThanTen;; val it : int array * int array =
([|20; 11|], [|5; 5; 6; 1; 3; 7|])
Some of the first element for which
the given function returns
Otherwise, it returns
Array.tryFindIndex works just like
Array.tryFind, except rather than
returning the element, it returns its index in the array:
> // Simple Boolean function let rec isPowerOfTwo x = if x = 2 then true elif x % 2 = 1 (* is odd *) then false else isPowerOfTwo (x / 2);; val isPowerOfTwo : int -> bool > [| 1; 7; 13; 64; 32 |] |> Array.
tryFindisPowerOfTwo;; val it : int option =
Some 64> [| 1; 7; 13; 64; 32 |] |> Array.
tryFindIndexisPowerOfTwo;; val it : int option =
Array.tryFindIndex illustrate why
Option type is so
powerful. In C#, functions similar to
tryFindIndex will return
−1 to indicate failure (opposed to
None). However, if you are trying
−1 could indicate either a failure to find
an array element or finding an element with value
Array module also
contains the aggregate operators of the
List module, namely:
iter. In addition, there are also
index-aware versions of these methods. Example 4-9 demonstrates the
iteri function, which behaves just
iter except that in addition
to the array element, the element’s index is provided as well.
Example 4-9. Using the iteri array aggregate function
> let vowels = [| 'a'; 'e'; 'i'; 'o'; 'u' |];; val vowels : char array = [|'a'; 'e'; 'i'; 'o'; 'u'|] > Array.
iteri(fun idx chr -> printfn "vowel.[%d] = %c" idx chr) vowels vowel. = a vowel. = e vowel. = i vowel. = o vowel. = u val it : unit = ()
Arrays are helpful for storing data in a linear fashion, but what if you need to represent data as a two-, three-, or higher-dimensional grid? You can create multidimensional arrays, which enable you to treat data as a block indexed by several values.
Multidimensional arrays come in two flavors: rectangular and jagged; the difference is illustrated in Figure 4-2. Rectangular arrays are in a solid block, while jagged arrays are essentially arrays of arrays.
Which type of multidimensional array to use depends on the situation. Using jagged arrays allows you to save memory if each row doesn’t need to be the same length; however, rectangular arrays are more efficient for random access because the elements are stored in a contiguous block of memory. (And therefore can benefit from your processor’s cache.)
Rectangular arrays are rectangular blocks of
in memory. Rectangular arrays are indicated by rectangular brackets
with a comma separating them for each new dimension. Also, just like
single-dimensional arrays, there are the
Array3D modules for creating and
initializing rectangular arrays:
> // Creating a 3x3 array let identityMatrix : float[,] =
Array2D.zeroCreate 3 3identityMatrix.[0,0] <- 1.0 identityMatrix.[1,1] <- 1.0 identityMatrix.[2,2] <- 1.0;; val identityMatrix : float [,] = <1.0; 0.0; 0.0] [0.0; 1.0; 0.0] [0.0; 0.0; 1.0>
> // All rows for columns with index 1 through 2 identityMatrix.
[*, 1..2];; val it : float [,] = <0.0; 0.0] [1.0; 0.0] [0.0; 1.0>
Jagged arrays are simply single-dimensional arrays of single-dimensional arrays. Each row in the main array is a different array, each needing to be initialized separately:
> // Create a jagged array
let jaggedArray : int = Array.zeroCreate 3jaggedArray. <- Array.init 1 (fun x -> x) jaggedArray. <- Array.init 2 (fun x -> x) jaggedArray. <- Array.init 3 (fun x -> x);; val jaggedArray : int   = [|[|0|]; [|0; 1|]; [|0; 1; 2|]|]