F# is a functional programming language that recently officially added to Visual Studio.NET supported languages. To me it reminds me alot to Python because of the indentation rules. If you want to play around with this language, you could either use:
What made me attracted so much to F#? It was caused by this informative, interesting and humorous session by Luca Bolognese. And yes, since he’s an Italian, his introduction to programming in F# is an offer that I can’t refuse!
In his talk, Luca highlighted three most important keywords in F#. They are:
- let. This keyword is similar, but different to var in C# or Dim in VB. But let is actually more on binding one object/value to a symbol. This symbol will be an immutable object, which means the its value/object can’t be change during its life-cycle.
- |>. This operator is similar to pipe in MS-DOS.
- fun. This keyword is used in creating lambda function.
Luca also highlighted that F# gives you the facility to immediately concentrate on the core solution of the problem. Because in F#, Luca said, we can program the solution of a problem just like the way we would solve it in our brain.
Anyway, let’s get dirty with simple example in F#:
01.
//bind x to an integer
02.
let
x = 10;
03.
04.
//create a list of integers
05.
let
data = [1;2;3;4]
06.
07.
//define a square function
08.
let
sqr x = x * x
09.
10.
//Luca notes that this method is something that we will do
11.
//in C#
12.
let
sumOfSquaresI nums =
13.
//"let mutable" will create a variable instead of binding to a symbol
14.
let
mutable
acc = 0
15.
for
x
in
nums
do
16.
//assign value to acc
17.
acc <- acc + sqr x
18.
//return the value
19.
acc
20.
21.
//This is the same function, but in a recursive function
22.
let
rec
sumOfSquaresF nums =
23.
//match is a branching mechanism plus binding
24.
match
nums
with
25.
| [] -> 0
26.
//h::t will match to a list
27.
//and bind symbol h to the first element of the list
28.
//and bind symbol t as the rest of the elements in the list
29.
| h::t -> sqr h + sumOfSquaresF t
30.
31.
//This is the F# way of doing Sum of Squares
32.
let
sumOfSquares nums =
33.
nums
34.
//Operator |> is similar to a DOS pipeline
35.
//Seq.map will apply the function sqr to each elements in the list
36.
|>
Seq
.map sqr
37.
//By now the list we have is a list of squares, e.g. [1;4;9;16]
38.
//Seq.sum will sum every elements in the list and return an integer
39.
|>
Seq
.sum
From above code, I hope we can quickly spot the difference between programming in imperative way (sumOfSquaresI) vs. the F#-way (sumOfSquares). Clearly, the F#-way gives us not many room to make mistake, and if you think of it, it is indeed how we would solve it in our brain (first, square every element in the list, then sum it up).
Another plus point is parallelization. Parallelization is very much working out of the box in F#. We can easily parallelize the function sumOfSquares by calling the parallel-version of the method.
1.
let
sumOfSquares nums =
2.
nums
3.
|>
Seq
.pmap sqr
4.
|>
Seq
.psum
About Hardono
Incoming Search
f#