New Programming Paradigm

Picture courtesy – Pixino

Introduction

Developers should never close the gates to learning a new thing. In this dispatch we look at alternative to Object Oriented Programming (OOP)! If this comes as surprise to you look at the popularity of JS, Python which eventually adopted OOP. Given that popular thinking of programming in a style different from OOP is impossible.

This is one obscure style gaining foothold steadily over time. For ages there was Functional Programming! For long time this was strictly limited to academic circles and few niche industries. Need for software and boldness of commercial aspects of software enterprises now encourages to bring such obscure paradigms to lime light.

Functional programming

Remember the day you started programming in OOP. You would have started with keywords as Classes, Interfaces, Data types etc. Conceptually you would have encountered keywords like Inheritance, Polymorphism, Encapsulation etc. Now when we are starting with Functional Programming (FP) paradigm what would be such keywords? Following is the list of such keywords –

Functions, is a program which generates an output for a given input [smiley]. In a simpler manner it maps an input set to output set
o Purity, is a property of function which indicates idempotence of operations performed within function and no side effects on the global state
Expressions, is an operation which produces a value as output always. So, this implies no null return values when compared to OO
o Referential transparency, is a property of expression that assures replacement of a value with expression that generates that value.

Immutability is an inherent behaviour in FP which is offered by default.

F#

Pronounced as F (Sharp) like we pronounce C# is a functional programming language offering of Microsoft. It is compatible for .NET framework. Like other languages of .NET framework code written in F# compiles to CLR of .NET framework. The best part is it is first class where .NET framework is supported. With steady increase of steam in .NET core’s popularity this language finds itself as first-class citizen in all places where .NET core is supported. It thus can target multiple runtimes and operating systems.

To entice you if you are just getting started with python for machine learning, do consider F# as a programming language. Beyond that one of the advantages for OO developers to give F# a try is – Coming from OO paradigm F# has many notions or ways to achieve something which you will easily relate to in OO code e.g. methods. F# being a functional programming language should ideally not have a notion of method. Similarly access to namespaces and related methods. Thus, this is an easy ramp up for OO developers who can then consider to stay or give other options in functional programming a try.

Let us get practical with two important aspects of functional programming.

Function

Functions are the key cogs in F# language. Let us see what we mean –

 

let getExponent base, exponent = base ** exponent

 

Being from OO background and for dominant part of career working with C# and Java, there is not much of ritual here. Function does not have a keyword to indicate that we are defining function. Usage of space should be done carefully. Parameters are separated by comma. There is no special reserved keyword used to indicate return value. These days even OO languages have defaulted to use the last string as return value. But that is syntactic sugar and more convenience rather than language feature. Here it is a language feature. The same function could be separated in two lines as follows –

 

let getExponent base exponent =

   base ** exponent

 

Did you observe? We cannot have a void return type or some code which does not return anything! Anyways, it is great that we have defined a function now let us take a look at how to invoke it –

 

let twoExpFour = getExponent 2.0  4.0

 

Notice the call, no round brackets, no comma seprating the parameters. Many of you might connect with this with VB language. Let me assert there is no such influence playing role here. In fact, this is how many other functional languages invoke functions.

Expression

Now let us talk about the next big thing considered in functional programming: Expression. An expression is already exposed above – base ** exponent. Another way to narrate our earlier observation is – So, the last expression evaluated in a function body is the return value of the function.

Interacting with console

We generally start with printing something to console. This time we stored in – let us say variables. Let us take it to a terminal where as human we can experience the outcome of an invocation –

 

printfn “Exponent result for 2 and 4 is %f” twoExpFour

 

 

Putting it all together

Though there are numerous tutorials on “How to” achieve something, we have added this section only for completeness. You can install F# while installing Visual Studio or by installing Visual Studio Code and adding extension Ironide plugin. Both IDE’s are great in terms of getting you started. You have project templates to create a new project for F# language. Once you create the project you will have default files like Program.fs. In that file you can add the above sections of code so that it looks like this –

 

open System

 

let getExponent base exponent = base ** exponent

 

let twoExpFour = getExponent 2.0  4.0

 

[<EntryPoint>]

let main argv =

   printfn “Hello world from F#”

   printfn “Exponent result for 2 and 4 is %f” twoExpFour

   0

 

This is great start however; we would need to go a bit deeper with this language before we attempt building a full-fledged system. F# taunts function to be first-class citizen. It however fails to gain much ground on pure function.

First class citizen – Function

First-class citizen is a term from language point of view it translates to following properties –

Function can return another function

This in code means –

 

open System

 

let boundedGenerator =

   let generatorFunc = fun ll ul -> [ll .. ul]

   generatorFunc

 

//Inside the variable main

let range = boundedGenerator

printfn “%A” (range 10 67)

 

//We could also print like this

System.Console.WriteLine(range 10 67)

 

There is this namespace usage which we used with “open” keyword. In this manner we can interoperate with larger .NET framework methods. We also used the inline anonymous function declaration using the keyword “fun”.

Function could be passed as parameter

All JS developers would be familiar with this property. This translates to –

 

let square = List.map(fun n->n**2)

 

printfn “%A” (square [3;4;5])

 

 

Function should be storable in any data structure of F#

This is straight forward to many developers from dynamic type system (now a days including C#) –

 

let someMathFuncs = [fun add n1 n2 -> n1+n2, fun subtract n1 n2 -> n1-n2]

 

 

Function should be identifiable

We have already used this and is straightforward –

 

let getExponent base, exponent = base ** exponent

 

 

We are on top of the content for this dispatch. We will pick this thread in a future dispatch and model a domain using F#. We will put that in perspective with another language like Javascript.