A Peek into Functional Programming

I’ve heard of functional programming for a long time when a friend kept talking about how amazing Haskell is. From the name itself, I really have no idea what is functional programming. But recently, I’m taking a course online which happens to introduce functional programming. So I’m trying to give my two cents on this topic here.

The first thing that the instructor emphasised is to forget about all other programming languages you learnt before. It’s true because it’s a completely new approach of programming compared to imperative programming. It’s not a new programming language. It’s rather a mindset. In a nutshell, functional programming is trying to solve the problem by finding the mapping of the same object. It does borrow the concept of mapping in Maths.

m : S ----> T

Therefore, FP will not change the value of original object but instead, create a new object with new value. This feature is called immutable. What comes hand in hand with this feature is no state, no memory space, no side effect.

Let’s appreciate the beauty of FP.

fun countup_from1 (x: int) = 
    let fun count(from: int) = 
            if from = x
            then x::[]
            else from::count(from+1)
    in count 1
    end

I highlighted keywords so that the syntax is easier to see. What is in ‘in’ section is what will be executed and it will get values from ‘let’ body. The helper function defined in ‘let’ body can be referenced in ‘in’ as well.

An interesting article shows the comparison of programming paradigm. https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/functional-programming-vs-imperative-programming

Leave a comment