Wednesday, January 22, 2014

An adventure journey of Functional Programming and F# in Visual Studio: Part 1 of 17

Hello there!

This blog of F# contains full (long) blog posts of adventure in functional programming and F#. I call it adventure, because I’ll try to make F# as fun to as possible to learn.


This blog is moved from MUGI blog, with some refinements. Also this blog is starting to use Visual Studio from Visual Studio 2012 and Visual Studio 2013 (from Release Candidate to RTM), and provide hints of F# 3.0 in Visual Studio 2012.

Now, here are the parts:

  1. Part 1: Introduction to Functional Programming (you’re here)
  2. Part 2: Functional Programming Concepts
  3. Part 3: Introduction to F# 2.0
  4. Part 4: Functions and Delegates in F#
  5. Part 5: F# standard libraries
  6. Part 6: OOP in F#
  7. Part 7: Using LINQ in F# (and the upcoming F# 3.0)
  8. Part 8: F# Asynchronous workflow
  9. Part 9: F# MailboxProcessor
  10. Part 10: Units of Measures
  11. Part 11: F# Power Pack
  12. Part 12: F# for Silverlight 4 (and above)
  13. Part 13: A look at F# 3.0 in VS 2012
  14. Part 14: A look of Functional Programming in VB 10 and C# 4.0 compared to F#
  15. Part 15: A look of F# compared to Haskell, Scala and Scheme
  16. Part 16: F# future and what features that F# must have
  17. Part 17: Retrospective

Part 1: Introduction to Functional Programming

To be honest, many developers especially in Indonesia don’t know much about this. But I bet all of us are already familiar with the concept, at least using it everyday.

But what is really, Functional Programming? Simply put, functional programming is programming with mathematical functions. The longer definition is, programming with emphasize on functions and higher order functions with side effects.

What is a mathematical function? A function in math is an operation that has parameters (1 to many) and return a value. In math, function can be called many times with the same parameter will always yield the same result.

We all know this in math:

y = x +1

and we can simply translate y into function of x:

f(x) = x + 1

So intuitively we can say

f(x) = y = x +1

Therefore distinction between function and data now becoming transparent!  

A practical intro to functional programming sample

Let’s dive into math samples, because basic (but fundamental) functional programming is closely connected to math. We are developers, right? then we are all familiar with basic concept of math.

From math to code, it’s fun!

An example of this is the factorial function: (using Piecewise notation)


Using F#, the code above is:


It is similar! Note the rec keyword, it means recursive function “marker”. More on this in Part 4. The “|” character in the sample above means pattern matching in F# (and also in ML family languages).

Another example is this notation of absolute (ABS) value function: (again, using Piecewise notation)


We all know that any value of x either positive or negative will return a positive of x, with the condition and operation as above.

For example:

|-6| = 6


|6|= 6

No matter how many times you call it, it will always return the same result. But we are more familiar with abbreviated function such as abs(x). Because you will find it’s easier to write in code, either Java, C++, C#, or Visual Basic using abs(x) instead of |x| because you will get syntax errors.

Then we can write:


Now, the function is easier to read and ready to implement on your code.

But since this blog entry emphasize on F#, this is the teaser code:


.. and the teaser code in VS 2012, with F# interactive window shown:


Now, try to write the above function in C# and VB.NET! And you’ll find that it’s quite noisier:

C#: introfsharp_vs2010_p2


VB.NET: introfsharp_vs2010_p3

Yes, C# and VB don’t have match expression, such as those in F#. But this match expression lays out the foundation of today’s modern functional programming languages, such as F# and other ML variants, including other non ML such as Haskell.

In fact, the F# code looks natural to the piecewise notation of absolute value above. And it has very succinct syntax. The term succinct syntax means less noise compared to the other languages such as C# and VB above.

But VB is more reasoned toward functional programming: the return type of the function is inferred from the operation inside of the function body, instead of C#! It’s closer to F#. Also it’s a clear definition in VB to distinguish function and procedure: function returns a value, procedure doesn’t return value and it is meant to do something that can have side effects.

So, a functional programming language is simply programming with math function.

As simple as:

f(x) = y

and y can be a form of an operations such as:

y = x + 1

To maintain consistency and guaranteed that the result will not change, the parameter of the function must be immutable, as seen in the above sample. This is different in C#, VB and others, where you can modify the value of the function parameter.

so, this expression is invalid:

x = x + 1

Why? Because there’s no x equals to x + 1 in math!

In functional programming, this expression:

y = x + 1

is really an equality notation. The equal sign means assigning an operation or value to a symbol, such as y. Therefore x is left unchanged.

Then why is this necessary? Not just this immutability, but there are other concepts as well. It’s way more older than most of us!

But why do I say, we already use it everyday? I’ll explain it later after the “why”.
A short history of functional programming

Based on this functional programming entry on Wikipedia, a functional programming was born from early work on calculus, especially Lambda calculus. Lambda calculus describes functions and their evaluations, including bindings and composition. Although it’s merely an abstraction, it’s definitely a baseline/foundation in modern functional programming.



C# and VB since Visual Studio 2008 (hence C# 3.0 and VB 9.0, respectively) has embedded this lambda calculus concept and wrapped it up as a nice lambda expression. F# has this since F# 1.0 back in 2005, when it was a research project in Microsoft Research. More on this on Part 13.

An early precursor of functional programming is Lisp in 1958. It has very succinct syntax, and it’s very close to F# syntax today. An example of Lisp:


There’s no return statement, the result is implied, just like F#.

Also notice the indentation. This indentation style is then will be widely used not just in Lisp and F#, but it’s also found in Scala.


    Sidenote on LISP:

For more formal standard documentation, see for ANSI LISP (also called Common Lisp). Lisp itself has been further researched and there is a derivative work of Lisp called Scheme. Yes, Scheme is another member of functional programming language family.

Lisp has been an inspiration for creations of other functional programming languages, not just Scheme. It’s also being used in other bigger software and more specific such as in AutoCAD began from AutoCAD 9 until now (called AutoLISP).

Further inspiration of Lisp is another variance of functional programming languages, ML (metalanguage) is born in early 1970s. Although it’s originally stated to investigate math theorem of LSF, it was then further researched and then implemented into Caml.


Caml (Categorical Abstract Machine Language) was developed continuously by INRIA, with helps from other large companies in a Caml Consortium. It has more implementations derivations, into OCaml.


OCaml adds object oriented constructs to Caml, this is why OCaml is abbreviated from Objective Caml.

OCaml sample: (one of OCaml IDE is Eclipse Indigo (a.k.a. Eclipse 3.7) with OCaml support as plugin)


Looks similar in F#? Yes, absolutely. This OCaml is the known source of inspiration of F#.

There’s a long history about ML, Caml and others but the big picture of this ML family language is this:


For more information about other languages in the picture above, visit and from there you will also find some links to other derivatives such as Miranda, created by David Turner in 1983 when he was working at Research Software Limited.


Miranda is also borrowing concepts from SASL (also from David Turner).

This development of Miranda has resulted in new creation of other functional programming language such as Haskell. And Haskell brings a concept of purely functional programming into practice, along with the side effect handling (this side effect is common in imperative programming such as Java, C#, VB.NET) and other concepts in lambda calculus.

But Miranda was also known for its neat implementation of lazy evaluation (also called non strict evaluation for the technique used to minimize repeated evaluations during runtime). This non-strict trait later influenced Haskell and made into OCaml.


Haskell has very interesting history. At first it was mainly used only in academic practice in 1990, now it’s been in use in a larger and commercial user base. Previously considered impractical for general purpose programming, but now it’s matured and it has very large documentation and support base.

Haskell is also the most pure functional programming language that made itself available on various platforms, not just UNIX and Linux. It’s available on Windows too, and Simon Peyton Jones has been porting Haskell to .NET, although currently it only supports Visual Studio 2005 and Visual Studio 2008.

Now, I have mentioned side effects, pure functional, immutabilities, pattern matching.. What are these?

Yes, those are functional programming concepts! Next part 2 is the concepts of those.


Further Reference links

List of further reference links:

  1. Microsoft F# on Microsoft Research,
  2. Microsoft Visual F# on MSDN:
  3. CAML official page on INRIA,
  4. OCaIDE, OCaml IDE support for Eclipse Helios and Indigo:
  5. Miranda official page:
  6. Haskell official landing page:

No comments:

Post a Comment