Visual Studio 2008 and F#

Once I downloaded and installed Visual Studio 2008 Team System in this laptop, one of the first things I did was to install F# to see how well it worked in the shiny new IDE. It worked without any hassle and to celebrate I wrote a typical business example: get the average salary of a group of employees.

First some preparatoy work: lines 3 to 7 define a F# record with 4 fields; lines 9 to 13 fill an employee list (not an array, mind you, but a variable length list).

The interesting stuff starts at line 15, the average function takes a list of numbers and returns, well, its average, in this way:

  • If the list is empty it just returns 0
  • In any other case it adds up all the elements in the list (the fold1_left function accumulates all the elements using '+') and divides the result by the length of the list.

Finally, the averageSalary functions takes a list of employees and:

  • Extract their salaries into a list (this is done by the salaries function which is defined locally inside averageSalary)
  • Apply the average function to the salary list

A conosseur may argue that there are better ways of implementing the salaries function, but I didn't want to burden you with more functional delicacies. So, what do you think of this way of programming as compared to C#, VB.NET or Java?

Published Sunday, November 25, 2007 10:20 PM by Edgar Sánchez

Comments

# re: Visual Studio 2008 and F#

Monday, November 26, 2007 5:03 AM by David Taylor

Hi Edgar,

While I appreciate you are *implementing* average in a functional way - I think the message will be lost on most C# programmers who will just look at your companyRoll assignment and figure in C# via a standard query operator they can do the same thing:

var averageSalary = companyRoll.Average(e=>e.salary);

Maybe you can do another example that is not one of the standard query operators?

# re: Visual Studio 2008 and F#

Monday, November 26, 2007 7:00 AM by Edgar Sánchez

Hi David,

Ah, LINQ! You've really got me here! I guess in my eagernes to show a simple example of F# in a "business scenario" I totally miss the LINQ forest for a few functional trees (well, lists...)

# re: Visual Studio 2008 and F#

Tuesday, November 27, 2007 6:59 AM by Robert Pickering

There's a couple of things you can do to make the F# more like the LINQ. First you can place most averageSalary function with a map then pipe the results of this to your average function:

let averageSalaries =

   companyRoll

   |> List.map (fun emp -> emp.salary)

   |> average

But the LINQ average function is very similar to the average function defined in the example, except the LINQ version except the LINQ version takes a project function, which can easily be added to the F# version:

let average numbers project =

   match numbers with

   | [] -> 0.

   | _ -> (List.fold_left (fun acc x -> acc + project x) 0.0 numbers) / (float numbers.Length)

let averageSalaries = average companyRoll (fun emp -> emp.salary)

And of course you can always use the LINQ functions from F#.

# re: Visual Studio 2008 and F#

Tuesday, November 27, 2007 7:54 AM by Anon

As someone with no knowledge of F# programming an a basic knowledge of languages like Java and C# I look at the F# code and say "What?!".

It is completely unclear to me that "average" is a function, what it's parameters are and what it returns.  Same is true for "averageSalary".

It's probably just that I don't get F# but it may as well have been written in Greek.  Even Perl would have been easier to understand.

# re: Visual Studio 2008 and F#

Wednesday, November 28, 2007 4:10 PM by Josh

I have to agree with Anon up there.

From some one who has used a lot of different languages, the syntax of F# definitely leaves something to be desired.

I thought that the days of writing new languages that use nothing but cryptic symbols was over...  I mean, you should not be able to write an entire line of code using all symbols: | [] -> [] is not acceptable.

Why use semicolons instead of commas to separate items in a list?

Where do the functions end and arguments begin?

# re: Visual Studio 2008 and F#

Thursday, November 29, 2007 9:42 AM by Nate

Yea, I agree with Anon...  I have no clue what it is doing.

But, the syntax does look somewhat clear.  I guess I'm just not sure how all the operators, et al work in F#.  I have purchased the book "Foundations of F#" but I haven't opened it yet (had it for a few weeks now).

Some of the syntax reminds me of Delphi, a little :)

# re: Visual Studio 2008 and F#

Wednesday, January 16, 2008 8:21 AM by Morten

Josh. F# seems to be a functional programming language with a syntax that looks a lot like ML. Judging from the syntax currying is supported so it is hard to distinguish between a function and its arguments.

Also [] -> [] is _very_ acceptable :-)

# re: Visual Studio 2008 and F#

Monday, February 11, 2008 12:34 AM by brian Leahy

To david taylor:

your c# example is a combination of functional language features recently added to c#.  Ironic.

# re: Visual Studio 2008 and F#

Monday, April 28, 2008 6:33 PM by Mike Gold

I think your sample is good, but judging from the feedback I saw,  I would heavily explain each section of your code.

e.g.

type employee, this is a type definition (similar to a struct)

companyroll, a linked list of instances of the employee type

let average = match(...

This is pattern matching in F#.  This section will average over an F# Linked List of numbers and obtain the average value

let averageSalary employees = rec salaries employees...

This is also pattern matching in F# over a list.  The purpose of this code is to take all of the salaries out of the list of employees and place them in a List of salaries. The function recurses through the list and concatenates the salary to the front of the list.

average (salaries employees) is an example of currying function.  the salaries recursive function is called with the list of employees and returns a list of salaries as floats.  average is then called on the list of salaries to obtain the average salary of the employees.

# re: Visual Studio 2008 and F#

Wednesday, March 28, 2012 2:27 AM by Brittany

And how does that mean? I do not understand anything.

# re: Visual Studio 2008 and F#

Wednesday, May 16, 2012 2:17 PM by XtmbVuftpjmYQfNUyYB

WRDGRA Very good blog post. Fantastic.

# re: Visual Studio 2008 and F#

Saturday, November 3, 2012 11:56 PM by QomvgodXkSGEvSQx

cC98D6 Appreciate you sharing, great post.Really looking forward to read more. Really Cool.

# re: Visual Studio 2008 and F#

Friday, February 1, 2013 10:59 PM by WMQwKYwNXsfSNJvMa

GRSigE Thanks again for the article post.Really looking forward to read more. Really Great.

# re: Visual Studio 2008 and F#

Thursday, February 28, 2013 1:41 AM by dBGuVvsIepmqCqmjH

P6Vo61 Appreciate you sharing, great article post. Will read on...