Tuesday, February 05, 2008

There are much better explanations by people who really know about functional programming so the next few posts on F# are more an exercise for my brain than anything else. But maybe you will find them useful...

So I haven't heard of the tuple since watching boring educational videos on database theory 10 years ago. But alas the tuple appears again as I'm learning F# and also digging into some mathematical concepts again. It's actually been there all along as I declared a singleton or joined database tables. A tuple is basically a finite sequence of objects that are in a particular order and can contain the same object more than once. It is also immutable (Aka, cant be modified). An example of a tuple could be a first name, last name and age. In C# we may define it as a generic triple (a tuple with three items) class as follows:

public class Triple<A,B,C>
{
    private A value1;
    private B value2;
    private C value3;

    public Person(
        A value1,
        B value2,
        C value3)
    {
        this.value1 = value1;
        this.value2 = value2;
        this.value3 = value3;
    }

    public A Value1 { get { return this.value1; } }
    public B Value2 { get { return this.value2; } }
    public C Value3 { get { return this.value3; } }
}

Then define our person as this triple:

Triple<string, string, int> person = 
    new Triple<string, string, int>("Richard", "Nixon", 61);

We could then set other values to the contents of the triple if we wanted:

string firstName = person.ValueA;
string lastName = person.ValueB;
int age = person.ValueC;

Easy enough right? Well lets see how we could do the same thing in F#. First off we don't have to create or instantiate a tuple class (This is actually done under the covers as we will see in a moment). We simply set the variable equal to a comma separated list of objects as follows:

let person = "Richard", "Nixon", 61

And voila! We have a tuple (A triple to be precise)... Now to set the contents of the tuple to other variables we simply do the following:

let firstName, lastName, age = person

Here we just set the individual firstName, lastName and age variables to the corresponding values in the tuple. On the other hand if we just want the first name we can block out the last name and age with the underscore placeholder ("_") and just get the first name:

let firstName, _, _ = person

or we can just get the last name and the age by blocking out the first name. You get the point...

let _, lastName, age = person

F# uses "pattern matching" to match up the variables listed, with the values in the tuple. Now as mentioned before, under the covers F# does actually use generic tuple types as seen when working with a tuple from F#...

in C#...

image

In reflector we can see that there are tuple definitions for up to 7 items:

image

Edgar Sanchez has a nice blog entry on tuples here.

F#
Tuesday, February 05, 2008 2:46:15 AM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
 Monday, January 28, 2008

Mike Jones has posted an analysis of an adaptation of the Concurrent Life app (That ships with the F# distro) here. I haven't gotten through the entire article yet but so far it is great. The sample app has a C# interface and consumes an F# library. He suggests that C#'ers not familiar with functional programming use F# imperatively to get comfortable with the language syntax then tackle the functional style.

Also, I came across these articles by Thomas Petricek that are proving very helpfull:

  • F# Overview (I.) - Introduction
  • F# Overview (II.) - Functional Programming
  • F# Overview (III.) - Object Oriented and Imperative Programming
  • F# Overview (IV.) - Language Oriented Programming
  • And also a nice intro by The Flying Frog Consultancy...

    C# | F#
    Monday, January 28, 2008 4:38:04 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  | 
     Friday, November 09, 2007

    I have been wanting to dig into F# for quite some time and finally got an opportunity. My first stab at it implements the calculation of kinetic energy given a weight in lbs, speed in mph and output in kJ. Pretty basic, but ya gotta start some where right? :)

    KineticEnergy.zip (.95 KB)

    // Get info from the user
    do Printf.printf "Enter the weight (LBS): "
    let m = read_line()
    do Printf.printf "Enter the velocity (MPH): "
    let mph = read_line()

    // Pound to kilogram conversion
    let kg lbs = lbs / 2.2

    // Miles/h to Meters/s conversion
    let mps mph = (((mph * 1.6) * 1000.0) / 60.0) /60.0

    // Kinetic energy calculation
    let Ek m v = (0.5 * m * (v * v)) / 1000.0

    // Show me the money
    do Printf.printf "\nEk = %fkJ\n\n" (Ek (kg (float_of_string m)) (mps (float_of_string mph)))

    // Wait! Let me see the answer...
    do Printf.printf "Press enter to continue..."
    let x = read_line()

    Friday, November 09, 2007 9:52:37 PM (GMT Standard Time, UTC+00:00)  #   |  Comments [0]  |