A tuple is a grouping of unnamed but ordered values, possibly of different types.
Examples of tuples include pairs, triples, and so on, of the same or different types. Some examples are illustrated in the following code.
You can use pattern matching to access and assign names for tuple elements, as shown in the following code.
You can use tuple patterns in let bindings as follows.
This binds values a and b at the same time. If you need only one element of the tuple, the wildcard character (the underscore) can be used to avoid creating a new name for a variable that you do not need.
The functions fst and snd return the first and second elements of a tuple, respectively.
There is no built-in function that returns the third element of a triple, but you can easily write one as follows.
Generally, it is better to use pattern matching to access individual tuple elements.
Tuples provide a convenient way to return multiple values from a function, as shown in the following example. This example performs integer division and returns the rounded result of the operation as a first member of a tuple pair and the remainder as a second member of the pair.
Tuples can also be used as function arguments when you want to avoid the implicit currying of function arguments that is implied by the usual function syntax.
The usual syntax for defining the function let sum a b = a + b enables you to define a function that is the partial application of the first argument of the function, as shown in the following code.
Using a tuple as the parameter disables currying. For more information, see "Partial Application of Arguments" in Functions (F#).
When you write out the name of a type that is a tuple, you use the * symbol to separate elements. For a tuple that consists of an int, a float, and a string, such as (10, 10.0, "ten"), the type would be written as follows.
int * float * string
If you are only using tuples from F# and not exposing them to other languages, and if you are not targeting a version of the .NET Framework that preceded version 4, you can ignore this section.
Tuples are compiled into objects of one of several generic types, all named Tuple, that are overloaded on the arity, or number of type parameters. Tuple types appear in this form when you view them from another language, such as C# or Visual Basic, or when you are using a tool that is not aware of F# constructs. The Tuple types were introduced in .NET Framework 4. If you are targeting an earlier version of the .NET Framework, the compiler uses versions of System.Tuple from the 2.0 version of the F# Core Library. The types in this library are used only for applications that target the 2.0, 3.0, and 3.5 versions of the .NET Framework. Type forwarding is used to ensure binary compatibility between .NET Framework 2.0 and .NET Framework 4 F# components.