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 : 🔥 → 🌽 → 🍿
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:
📭.hasMail() → ✅
In the non-functional language example above, the functions
hasMail are part of the mailbox data-structure itself. A functional equivalent might look like this:
sendMail : 📭 → ✉️ → 📬
hasMail : 📬 → ✅
Our functional equivalents of
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
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.
badJoke : 😀 → 😐
scaryStory : 😐 → 😱
writeBlogPost = badJoke >> 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 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.