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?