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 Tuesday, February 07, 2012 5:46:22 AM (Pacific Standard Time, UTC-08:00)
If feel a bit behind and need to catch up on WPF, this is the book.
Great book on F# containing from Beginner to Advanced. It even has chapters on more arcane features of the language, such as Computation Expressions and Quotations.
Because this book provides source code in Standard ML, it's a fantastic resource for learning F#. One bit of warning: this book does not teach classic data structures. While structures such as binomial heaps and red-black trees are presented, it is assumed that the reader already knows and understands them.
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.