F# Elegance for the VB Proletariat Part 1

I’ve been programming Visual Basic for eight years and haven’t really found a reason to switch to another language.  I’m certified in Java, written components in C#, done a website in PHP.  None of these languages offer anything that VB can’t already do.

But when a language comes along that does allow me to do things I can’t do in VB, I sit up and pay attention.  I’ve fooled around with Lisp, OCaml, and Mathematica, but the lack of .Net support has always been a deal killer.  Well with the new F# developer center going up and the CTP becoming available I’ve decided to get serious with F#.

Here’s a link to a quick tutorial to get you started.

http://blogs.msdn.com/chrsmith/archive/2008/05/02/f-in-20-minutes-part-i.aspx

I’ll do my own quick tutorial and start by analyzing a function that really shows off the power of F#.

 

let rec map(f : ‘a -> ‘b) (l : ‘a list) =

    match l with

    | h :: t -> f h :: map f t

    |[] -> []

   

let result = map (fun x -> x + x) [1;2;3]

printfn “%A” result

System.Console.ReadLine()

No need to panic!  We’ll take it slow.

 

let rec map(f : ‘a -> ‘b) (l : ‘a list) =

 

We’re basically defining a function called map.  Let’s break it up into its parts:

 

·         let rec introduces our function as a recursive function.  Functions in f# are not recursive by default.

·         map of course is the name of our function

·         (f : ‘a -> ‘b) is the first parameter of our function.  The parameter is a function that takes some value ‘a and transforms it into some value ‘b

·         (l : ‘a list) is the second parameter of our function.  The parameter is a list of values that match the input to our first parameter function.  They must all be ‘a as well.

 

match l with

 

Really just a select case statement that does one case if the list is empty and another case if there are still values in the list.

 

| h :: t -> f h :: map f t

     

This is our “still an item left in the list” case

 

·         | is the case statement.

·         h :: t decomposes our list into a head and a tail.  Sort of like popping at item off a queue.

·         ->  says now go do something

·           f h :: map f t applies our function(f) to the head of our list(h) and calls our function map recursively with the tail of our list(t)

 

|[] -> []

 

There are no items left in our list so end the recursion by return an empty list.

 

let result = map (fun x -> x + x) [1;2;3]

 

·         let result will hold the result of our function

·         = will bind the result of our function to the result identifier

·         map calls our function

·         (fun x -> x + x) is our function that adds a value to itself. Function(x) x + x in VB parlance.

·         [1;2;3] is our list of values that will have our (fun x -> x + x) applied to it.

 

printfn “%A” result

 

Prints our results to the console.

 

System.Console.ReadLine()

 

Does what it always does (keeps the console open so we can see our results)

 

We run our program and take a look at the output

 

 

Mission accomplished.  (1 + 1) = 2, (2 + 2) = 4, (3 + 3) = 6.  We’ve applied our “add a number to itself” function to each value in our list.  And all without the use of a single curly brace!  Next time we’ll do the same thing in VB and let me tell you, it ain’t pretty.

2 comments to F# Elegance for the VB Proletariat Part 1

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>