In F#, a tuple3 is concisely declared as a let statement with a single name and multiple values separated by commas.
Notice that F# infers the type of pair to be int * int. The asterisk (*) doesn't actually mean multiplication in this case. Instead, it indicates that the two types on either side are bound together as one type.
Tuples can contain any number of values, and the values don't have to be of the same type.
Tuples can be compared for equality.
And other comparisons are also legal.
However, tuples with different types cannot be compared. Trying to compare pair, which is of type int * int, with a tuple of type int * string results in an error:
In addition, tuples of different lengths cannot be compared.
Interestingly, in the above code, the F# compiler doesn't bother inferring the types in the tuple, (0, "F# Rules!"). It is left generic: 'a * 'b. The F# compiler sees that the tuples have a different number of values and stops.
Next time we'll look at some cool ways to use tuples in F# programming.
1Please don't hurt me Keith! 2Usually while sucking down a can of Schlitz. 3too-pull
let inline (<!) x y = match x, y with | (x1,_),(y1,_,_) when x1 < y1 -> true | (x1,_),(y1,_,_) when x1 > y1 -> false | (_,x2),(_,y2,_) when x2 < y2 -> true | (_,x2),(_,y2,_) when x2 > y2 -> false | _ -> true
Page rendered at Saturday, October 11, 2008 1:52:53 AM (Eastern Standard Time, UTC-05:00)