Introduction to Functional Programming and FSharp for CSharp developers

As programmer/developers, you must know more than one type of programming language. I would say learning at least one Functional programming language will change your view to look at the problems. As we all C# based programmer knows that C# language is based on Object Oriented principle while the FSharp(F#) is based on completely different paradigm. In this blog post we’ll talk overview of functional programming, features, Introduction to F#, Success story and familiar terms of FSharp(F#) for C# developers.

Functional programming

where functions are first class citizens.

“In computer science, functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions.” – Wikipedia

Here are some of least features a functional language must provide:

  • FirstClass functions
  • HigherOrderFunctions
  • LexicalClosures
  • PatternMatching
  • SingleAssignment
  • LazyEvaluation
  • GarbageCollection
  • TypeInference
  • TailCallOptimization
  • ListComprehensions

These features enable or support the following aims:

  • shorter programs (lower lines-to-effect ratio)
  • program correctness
  • expressive programs
  • Immutability

Simple Principle

As illustrated in figure above, the function takes and input and generates output. Every time you give input you can exactly one output, event after repeated over time. This is known as Idempotancy. The functional programming is more of dealing with Immutable.

The functional programming is being used in Academic and Industrial engineering since a long time. The “Erlang” is pure functional programming and became famous when robust and distributed systems were designed with the pure functional programming language. Developed and used by first Ericsson to build a fault –tolerant telecommunication systems, it’s not can be seen many famous applications systems like WhatsApp, RabbitMQ server, Facebook.

FSharp (F#)

Introduced in 2005 by Microsoft and supported by open source communities, F# consists of not only functional programming paradigm but also have flavor of paradigms of Imperative, Object, Asynchronous, Parallel programming including Metaprogramming so also known as Multi paradigm language. F# has a great part of open source community contribution which made it cross platform language for .Net Framework, Mono, Javascript.

Current stable release – 4.0 July, 2015.

Supported development tools

  • Visual studio
  • Mono Develop
  • Xamarin Studio
  • Sharp Develop
  • LINQPad

Official website -

Enterprise level success story

Recently, I visited development center (Hobokane, NJ) at their second technology meetup. is a fastest growing popular ecommerce website to save more money to customers. The CTO Mike Hanrahan detailed out the high level of a progressive e-commerce system they built.

Mike shared their story of using F# as the backend language to build a smart “Reduced Price Calculation” engine to serve end customer with best savings. With combination of Event sourcing and immutable event buses they built a robust system using F# and other technologies. Readout the success story for more details about the solution at Microsoft site.

For C# Developers

Does this look familiar?

Well here’s couple of things I would like to mention about F# which would sound familiar:

  • Type inference (var in C#)
  • Method chaining, Aggregations (Linq in C#)
  • Access to the .NET Framework and any managed code assembly
  • Anonymous types
  • Type Extensions
  • Encapsulation
  • Lambda Expressions

F# allows you to create either a project or simply a script. So there could be libraries you can built or simply just write ‘.fs’ ‘.fsx’ script. Also you don’t have to run/debug the whole program every time. Visual studio provides a nice Interactive window to run F# code interactively. In next post we’ll start with writing our first program and understanding few basics. I’m pretty excited to start learning a new language as I mentioned in my Recap 2015 blog post. For now,

let Say() =

    printfn "Hello world to first F# function"

Interactive console -

> Say();;

Hello world to first F# function

val it : unit = ()


Hope you enjoyed reading the introduction and if you’re excited then stay in touch by subscribing via email/rss to my blog or follow me on email for upcoming posts on learning FSharp.

More readings –

No comments:

Post a Comment