Hello again, Ama is here. Welcome back to my blog. It has been a year since my last blog post, sorry for not updating frequently. The college’s stuff and summer internship have been killing overwhelming me. Right now I want to share some of my notes on learning about Functional Programming in Haskell for the past 4 weeks.
First of all, what is functional programming?
Taken from here, functional programming is a programming paradigm for developing software using functions. It classified as a declarative paradigm because it relies on expressions and declarations rather than statements.
The specialty of functional programming is it follows the way of human thinking. We tend to process things inside of our minds by knowing the input and output of a problem, and then solve it by seeking the relationship between them. For example, if we want to know the length of a list, we have to sum the number of objects inside the list. Instead of iterating over the objects one by one, we could just map the objects as 1
and then sum them.
def sumLength(aList):
answer = 0
for element in aList:
answer += 1
return answer
sumLength :: [a] -> Int
sumLength aList = sum (map (\x -> 1) aList)
We can see that in procedural thinking, we define the sumLength
by telling the program to iterates over the elements of the list, increments the counter (which is the answer
) for each element, and then return the answer after all of the elements is iterated. While in functional thinking, we define the sumLength
by telling that sumLength
is a function that maps all the elements of the list as 1
and then sum all of the 1
. The conclusion is procedural programming language tells the computer what to do steps by steps while the functional programming language defines functions that are needed for the computer, that’s the big difference between them. The next thing that I learned in functional programming is there are no for or while loops. Instead, we use recursion in defining the functions.
One of the programming languages that support functional programming is Haskell. Even Haskell itself stated that Haskell is an advanced, purely functional programming language. For the first 4 weeks of learning to code with it, I found it is a little bit harder to switch from the procedural thinking to functional thinking because I have a tendency to see the concrete solutions rather than the patterns and abstractions of the solution. All of the functions that I learned may seem to be magical at first glance, so I have to try to code it for my self to understand them. I have done some of the exercises in here.
From the exercises, I found that we could use list comprehension in Haskell, just like in Python. With some notes that list comprehension in Haskell has no loops and just use the notation <-
to define the range of the list. For example, a function that increment each of the elements inside of the list.
incList aList = [aNum + 1 | aNum <- aList]
incList' aList = map (+1) aList
That’s all that I want to share in this post. I hope that I can understand the functional programming thinking paradigm the more that I code.