Introduction To Functional Programming And F#

As programmers/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 of looking at problems. As all C# based programmer know,  C# language is based on Object Oriented principles while the FSharp(F#) is based on a completely different paradigm. In this blog post we’ll talk about an overview of functional programming, features, Introduction to F#, success stories and familiar terms of FSharp(F#) for C# developers.

Functional programming

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 the minimal 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

    Function

As illustrated in  the above image, the function takes input and generates output. Every time you give input you get exactly one output, event when repeated over time. This is known as Idempotancy.

The functional programming is more about dealing wi the Immutable.

The functional programming has been used in Academic and Industrial engineering for 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. First developed and used by Ericsson to build a fault –tolerant telecommunication system, it can be seen in many famous applications systems like WhatsApp, RabbitMQ server, Facebook.

FSharp (F#) – where functions are first class citizens.

Fsharp

Introduced in 2005 by Microsoft, F# consists of not only functional programming paradigms but also has the flavor of paradigms of Imperative, Object, Asynchronous, and Parallel programming including Metaprogramming also known as Multi paradigm language. F# has a great part of open source community contribution which made it a 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

Visit the Official website.

Enterprise level success story

Recently I visited the Jet.com development center (Hobokane, NJ) where they held their second technology meetup. Jet.com is a fast growing popular ecommerce website that saves customers money. The CTO Mike Hanrahan detailed out the high level of progressive e-commerce system they built.

recap

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

For C# Developers,

Lemda

Does this look familiar?

Well here’s couple of things I would like to mention about F# which will 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 build or simply just write ‘.fs’ 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 the next post we’ll start with writing our first program and understanding a few basics. I’m pretty excited to start learning a new language as I mentioned in my Recap 2015 blog. For now,

  1. let Say() =  
  2. printfn"Hello world to first F# function"  
Interactive console -
  1. >Say();;  
  2.    Hello world to first F# function  
  3.    val it : unit = ()  
  4. >  
Hope you enjoyed reading the introduction and if you’re exited keep in touch by subscribing via email/rss or follow me on email for upcoming posts on learning FSharp.
 
Read more articles on FSharp(F#):