Friday, January 28, 2011

F# versus Scala

Both languanges IMHO provide similar functional programming features. And both are not pure functional programming language as they also support imperative and object-oriented style of programming.
Since I am not an expert with either programming language, I'm not qualified to provide detailed comparison of their features.
My expertise is more in OO software development, primarily in C# and prior to that in Java. I discovered functional programming quite by accident through a Microsoft pdc2008 presentation: An Introduction to Mcrosoft F#, by Luca Bolognese (back then the Principal Languages PM at Microsoft). That was my aha moment for functional programming, in particular F#. I have been hooked since and continue my exploration of this exciting landscape.
Coming from OO world when we often have to struggle with side effects, functional programming paradigm which avoids side effects of state and mutable data is a breadth of fresh air.
Given the plethora of functional programming languages out there, I would like to offer my two cents when the choice is between F# and Scala.
In my opinion, F# feels more functional than OO because its expression is closer to a mathematical expression. Scala on the other hand, feels more OO than functional.

For example, take this simple expression to increment an integer.
In F#:
let increment x = x + 1

In Scala:
def increment(x:Int) = x + 1

Due to type inference support in F#, there is no need to provide the type of the parameter x. In this case, the F# expression is more succinct.

Take another example, this time the expression involves Lambda expression.
In F#:
let list = List.map (fun i -> i + 1) [1;2;3]

In Scala:
val list = List(1,2,3).map( x => x + 1 )

In this case, the Scala expression is probably easier to understand than the F# one because it is more OO.

I personally prefer the approach that F# takes because the expression is more succinct. Although I must admit at times it takes some getting used to, especially when coming from OO background as I am.
But either language is a good choice to ease your way into functional programming from OO programming.

No comments:

Post a Comment