Intro to Functional Programming, With Emojis π°πΊπ₯

by John Palgut

Preface: The intention of this article is to provide an interesting and simplified starting point for introducing someone to functional programming topics. It is not intended to be a mathematically provable language or even a full featured programming language.

Before we begin discussing functional programming, we should first define our emoji language. Functions (which are very important in FUNCTIONal programming) in our language look like this:

functionName : parameter β result
goToWork :Β β° β π°
goToTheBar :Β π°Β βΒ πΊ
makePopcorn : π₯ β π½ β πΏ

The function goToWork takes β° as a parameter and returns π°. goToTheBar takes π° as a parameter and returns πΊ. makePopcorn accepts two parameters, π₯ and π½, then returns πΏ.

One important feature of functional languages, is the separation of data from it’s behavior. In other languages, especially object oriented ones, data and behavior are often tightly coupled. Lets take a look at an example:

π­.sendMail(βοΈ)
π­.hasMail() β β

In the non-functional language example above, the functions sendMail and hasMail are part of the mailbox data-structure itself. A functional equivalent might look like this:

sendMail : π­ β βοΈ β π¬
hasMail : π¬ β β

Our functional equivalents of sendMail and hasMail take the π­ and βοΈ as parameters, rather than being part of the π­.

Many functional languages have immutable variables and values. Immutable values cannot be changed once they are created. They can be used in functions, used to calculate new values, and even “modified”. However the original value will always remain as it was.

myCorn = π½
makePopcorn : π½ β πΏ
myPopcorn = makePopcorn myCorn
myPopcorn β πΏ
myCorn β π½

In the above example we declare a variable myCorn and pass it to the function makePopcorn. In the real world heating up corn mutates it and turns it into popcorn. In our functional language myCorn is immutable remaining as π½ after being passed to makePopcorn.

Functional languages support passing functions as parameters. Passing functions as arguments doesn’t automatically call the function, you’re actually passing a reference to the function. map is a function that takes function and a list as parameters. It then passes every value in the list to the function, then returns a list of the results.

iHateCoconut : π₯₯ β π₯
map iHateCoconut [π₯₯, π₯₯, π₯₯, π₯₯, π₯₯] β [π₯, π₯, π₯, π₯, π₯]

In the above example we use map and the function iHateCoconut to turn a list of π₯₯ into a list of π₯.

One of the draws of functional programming is that you can treat functions like Legos, putting them together to create completely new structures. This is called function composition. This is so common in functional languages that some include a built in operator to make the process easier.

scaryStory : π β π±
writeBlogPost : π β π±

The final functional programming topic I’d like to cover in this article is partial application. Partial application is when you pass a function less parameters than it needs. This creates a new function that takes the remaining parameters, but has the initial parameters fixed.

combineThings : π°οΈ β π±οΈ β π
cook = combineThings π₯
cook π½ β πΏ
cook π₯ β π

Above we have a function combineThings that takes two things of different types and then combines them. We partially apply combineThings with π₯ to create a new function cook. We can then use the new cook function to cook various different foods.

I encourage everyone to give a functional language a try at least once in their programming careers. Even if you find the style is not for you, it will change and improve the way you think about programming problems. If you’re looking for a language, I recommend Elm. It is fairly easy for beginners to pick up and has an active and helpful community. Additionally, the language used in this article is heavily inspired by Elm.