Tuples in F#

For those of you that are familiar with some of the language extensions available in C# 3.0, C#'s anonymous types are its implementation of the concept of tuples. For those of you that aren't, no worries, tuples are very simple.

In mathematics, a tuple is simply a sequence of objects, each of a specific type. In computer science, tuples are implemented as a very simple but useful data structure. For instance, lets say we had the following data we'd like to store:

School: Ohio University
Mascot: Bobcat
Year founded: 1804

Using F#, we could create a tuple grouping this data together like this:

>let mySchool = ("Ohio University", "Bobcat", 1804);;

When we enter this into the Interactive we get the inferred type information:

val mySchool : string * string * int

So our value mySchool is now bound to a tuple of type string * string * int. A tuple with three items in it is referred to as a triple. Some of the other terms are single, double, quadruple, etc... Or if you had a tuple with n items in it, you could just say n-tuple.

Tuples are very useful for returning multiple values from functions, here's an example of how that might work:

>let getSchool =
     let schoolname = "Ohio University" in
     let mascot = "Bobcat" in
     let year = 1804 in
     (schoolname, mascot, year);;

If you wanted to get your values out of your tuple you could use patterns (more on these later) like this:

>let x,y,z = getSchool;;

Now the identifiers x,y,z are mapped to their respective values contained in the tuple. If we did this:

>x;;

We'd see that x is now bound to the string "Ohio University".

Comments
IvanH  3/10/2008

Glad to see another F# site. Keep up the good work.
Asaad  7/14/2009

thanks alot
FP IS FOR IDIOTSS WHO DON'T GET TEH OBJEC ORIENTATION  11/6/2009

WHAT THE FUCK IS THIS SHIT
Arjun  1/12/2010

fine really cool.. keep up
hbcurry  4/12/2010

So this is basically Haskell... but Microsoft-ised?
George  4/24/2010

I love learning F# then ROFL'ing when I get to the comments
Scott Nickel  5/11/2010

This is crazy stuff. Many people have compared this to Lisp...not even close. Thanks for the articles....I am still confused...I have only been programming for 25 years...so I guess i still have a lot to learn. HAHA. Thanks for the site!
Scott Nickel  5/11/2010

By the way, I am still laughing about the guy whoe posted "What the F!!! is this S!!!" Pretty funny. It must have value, although I cannot see it at the moment. I will stick with C#.
Scott Nickel  5/11/2010

By the way, I am still laughing about the guy whoe posted "What the F!!! is this S!!!" Pretty funny. It must have value, although I cannot see it at the moment. I will stick with C#.
Scott Nickel  5/11/2010

By the way, I am still laughing about the guy whoe posted "What the F!!! is this S!!!" Pretty funny. It must have value, although I cannot see it at the moment. I will stick with C#.
Martin Stauber  6/24/2010

F# is a declarative language, which is in a completely different paradigm than any OOP language (imperative). The difference between the two is, in an imperative language, such as C# or Java, you specifically define the program flow. Each statement will change the program state. In a declarative language, such as F# or SQL, you describe logic without describing program flow. Basically, you describe what you want, not how to get it. A prime example of this in SQL is query plans; if you have ever messed with databases, you can write a query, then see what its query plan will be. The Query plan is the actual program flow (containing iterators such as merge joins, hash joins, nested loop joins, aggregates, seek and scans on indexes, etc.). Nothing in the Query text describes which type of join to use (in terms of hash, merge, or loops, etc.), just that we want to do a join.
Martin Stauber  6/24/2010

Also, some functional language (i believe f# included), have a really cool feature when doing recursion called 'tail recursion'. Have a look into it, it can make elegant recursive algorithms as memory efficient as iterative ones (if designed properly)
Leave Us a Comment!
Title (we need it)

Your Name (we need it)

Your Comment (we need it)