Article ID: 2844310 - View products that this article applies to.
Collapse this tableExpand this table
Collections in F# is not different from .NET collections. But in F#, by default the collections has this behavior concerning the nature of functional programming languages:
Currently in F# 3.0, the collections are:
Sequence in F# is basically the same as IEnumerable in .NET BCL. Therefore, any .NET type that implements IEnumerable is a sequence.
Enumerating sequence is computed when needed, and it's evaluated later therefore it is also lazily evaluated. This evaluation strategy is often called lazy evaluation, and it is therefore effective for the case of not evaluating all of the sequence elements.
Sequence operations are in Seq module in F#. The operation of map, filter, and many more has the same result as in LINQ. But sequence has more operations such as fold, unfold, and many more.
Sequence can be expressed as sequence expressions. For example, to express sequence that has number ranges:
It can contain stepping increments. For example:
The seq2 will contain 0, 10, 20, 30 to 100 respectively.
Because of the nature of lazy evaluation and not all of the elements are evaluated, sequence can contain infinite number of elements.
Note: this feature of infinite number of elements are not available in .NET BCL and also in C#/Visual Basic. Also doing interoperability while operating with infinite sequence in F# could have undesired result on C#/Visual Basic.
For example (from MSDN Library):
Sequence operations are available in Seq module in F# Core Library.
List is an immutable collection of elements and it is ordered. The term ordered means the elements in the collection is indexed, not sorted.
To create a list, the syntax is different from creating sequence. The elements are enclosed by "[ ]".
Example of creating syntax:
There is also alternative syntax by separating the elements with line break, for example:
Because List is also ordered, therefore there are other interesting features of List in F# to use as well.
Lists can be concatenated as long as the types are the same by using the "@" operator, for example:
You can attach elements to a list using :: (cons) operator. Again, the elements type has to be the same.
These are the properties of List:
Collapse this imageExpand this image
List in F# can be used and accessed using List properties of Head and Tail, in the form of pattern matching.
For example below, the code will recursively sum the head with the rest of the elements:
This head and tail pattern matching is very useful to examine a list not just recursively, but it can be iterated with index using List.iteri function built in F#.
List is available on List module in F# core library.
According to MSDN Library, F# array is:
"Arrays are fixed-size, zero-based, mutable collections of consecutive data elements that are all of the same type."
One other thing is, all elements in an array has indexes, therefore they are ordered.
To create an array, the elements are enclosed in "[| |]" symbols.
This is the sample to create new array:
And we can also use the line break as separator like List, for example:
Array is available on Array module in F# Core library.
Set and Map are unordered collections. This means the elements do not preserve the order of the collections as they can be changed by any operations on Set and Map.
Set is a collection of elements with the same type, just like Seq, List, and Array. Set can be constructed by using existing elements of sequence, list or array.
For example, constructing Set from Sequence:
Symbol b will contain a set elements with values of 5 to 15.
There are other functions that perform features unique to Set such Set.difference, Set.compare, Set.add, Set.intersect, Set.isSubset just like Set operations on math.
Map is just like Dictionary on .NET BCL, a collections of value pair of Key-Value. The Key must be unique, but the value does not have to be unique.
Initializing Map is quite similar with creating Dictionary. For example: (taken from F# Wikibooks)
Set and Map are available in Set module and Map module in F# Core Library.
This is the official page of F# Collection on MSDN Library: http://msdn.microsoft.com/en-us/library/hh967652.aspx
This is a community Wikibooks page of F# Set and Map explanation: http://en.wikibooks.org/wiki/F_Sharp_Programming/Sets_and_Maps
Community Solutions Content DisclaimerMICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.
Article ID: 2844310 - Last Review: May 7, 2013 - Revision: 2.0