Arrays in F#

Yesterday I wrote about list in F#. Today I’ll write about arrays, which unlike lists are a mutable flat storage and cannot be resized. That means you have to create a new array if you want to remove or add elements. Advantages include constant look-up time and the fact that they can store a large amount of data.

You can create a literal array in a similar way with the lists, placing the elements between [| |]:

The empty literal array is [||].

To create an array you can either use Array.create or Array.init. They both create and initialize an array, but the second makes a lambda expression, which allows advance initialization possibilities. The following creates an array with 10 elements initialized to 1:

Here is the output:

The same can be achieved using Array.init:

But we can use Array.init to initialize the elements from 1 to N for instance:

The arrays are mutable data structures. Elements are accessed with .[] or .(). The following code shows how to set the elements of an array:

You can iterate over the elements of an array with Array.iter and Array.iteri, the second also providing access to the index of the elements.

Retrieving the length of the array can either be done with Array.length arr or with arr.Length.

Like the lists, arrays provide mapping that creates a new array by applying a function on all the elements of an array (with or two arrays (with Array.map2).

A copy of an array can be done with Array.copy.

Appending elements to an array is also possible with Array.append, but the result is a new array, created by concatenating two arrays.

The last operation of arrays I’m going to mention here is the folding, which allows applying a function to all the elements of an array, threading an accumulator argument in the process. The following example shows how to compute the sum of the elements of an array.

Hits for this post: 38425 .

7 comments untill now

  1. Gravatar
    stringer @ 2008-11-20 04:47

    let sum1 = (Array.fold_left (fun acc x-> x + acc) 0 data9)
    let sum2 = (Array.fold_right (fun acc x-> x + acc) data9 0)

    Why is the prototype different for this 2 functions ?
    I mean fold_left acc arr and fold_right arr acc ?
    I thought the difference was only in how you move in the array..

  2. Gravatar
  3. Gravatar

    Hi, I must receive the data (an unknown number of obj)
    Considering that I will have access to the input in order:
    1- what’s most efficient to use between list and array??
    2- Using the attribute is equivalent to using a normal array?

  4. Gravatar

    Arrays are fixed size sequences. If you don’t know the length in advance, then you ought to use a list, which is a variable size sequence. Unless you want to allocate an array big enough to hold all the objects, but that will probably mean waste a memory. So it depends what kind of efficiency you are looking for.

  5. Gravatar

    yes, i know. But this isn’t a problem because the size is fixed by the sender; there isn’t waste of memory.
    I declare: let x (param: Object[] or Object list) and then flow once the seq.. The sender can allocate an array of 3 or 50 Object or (equivalently??) a linked list..
    Then, I’m interested to the “ParamArray” attribute: allows only a differente syntax or are there differences in terms of efficiency?

  6. Gravatar

    Again, depends on what kind of “efficiency” you’re looking for. Arrays are accessed through an index, so access time is constant; on the other side, linked lists had to be traversed each time you need to access some element. On the other side, lists are immutable, which means whenever you make a change (add, remove a node) a new list is created. Arrays are mutable.

  7. Gravatar
    Guillaume @ 2011-03-17 15:41

    Hi, I need to do an Array of struct. Is it possible? And how? Thanks.

Add your comment now