I switched to programming from the time I learned, access to computers has been more than 2 years. But officially embarking on Android programming only over a year ago. As an outsider, I have to learn and plow quite a lot. In a time of tinkering and learning more, I ran into Functional Programming. Oh no! She has very few Vietnamese articles to read, there are also few discussions on her on forums, most of which is the comparison between Object Oriented Programming (OOP) and Functional Programming (FP). So I wrote this series to lead the brothers to the spiritual path, the Functional Programming school
The world pioneered Computer Science
Two priests at the level of Hoa Than Ky
In a vast and unpredictable world of Computer Science , there are two approaches to computational processing in computers, also known as computational models:
- One side is Alan Turing ‘s Turing Machine (Developed 1928-1929).
- One side is Alonzo Church ‘s Lambda Calculus (Developed from 1935-1937).
These two models were created to solve the problem created by the German Mathematician David Hilbert . This problem is called the Decision problem :
Is there an algorithm that takes the input of a statement logically written and produces an always correct “yes” or “no” answer?
Of course, the mathematician Alonzo Church had come up with a solution to this problem earlier. He developed a system of mathematical expressions called Lambda Calculus ( Lambda Calculus ). Lambda analysis is basically a system of mathematical expressions using functions , application of functions, and recursion .
Mathematician Alan Turing proposed a hypothetical calculator called the Turing Machine that provided a simpler, yet powerful, computational model.
Basically it’s the equivalent of wearing function with Lambda Calculus of computing power but of mathematical techniques of Alonzo Church Lambda Calculus are difficult and confusing to apply.
Both the premise that what today we call the Computer Science (Computer Science) It is also the theoretical basis that forms the two extremes in the programming model ( Programming Paradigm ).
So what is the programming paradigm ( Programming Paradigm ) method?
There is no official definition for programming model (Programming Paradigm) but everyone understands that this term is used to refer to a programming style (style of programming). It is not for a particular programming language but it is about how you program, how you solve some problem .
Two extremes and sects
So how many schools of Programming Paradigm are there ?
The answer is many, but there are two main groups:
- Declarative Programming (DP) – Programming declaration
- Imperative Programming (IP) – Command programming
With imperative programming, things are usually associated with OOP, or object oriented programming. Speaking of declarative programming, people will refer to FP functional programming , also known as functional programming .
In essence, Imperative Programming or Declarative Programming is collectively called Programming Paradigm or a programming model . Specifically, you see the model that synthesizes the relationship between programming mechanisms in the book “Programming Paradigms for Dummies: What Every Programmer Should Know” by Peter Van Roy below:
- Imperative programming : telling the “machine” how to do something, and as a result what you want to happen will happen.
- Declarative programming : telling the “machine” what you would like to happen, and let the computer figure out how to do it.
From the grammar lessons we already know 2 types of sentences: Declarative Sentence , and Imperative Sentence .
I left the original English text because it is too concise, short and easy to recognize the difference. However, you will also see the keywords ” what “, ” how ” in bold here.
Translating the programming language command is that you tell your computer to do A, B, C … X, Y, Z. The results, you have in mind, now is to see if the machine returns the results you want.
In my opinion, most of them do not, after fixing n times the bug will get the desired result. This is because human brains, not computers, are running wrong.
Programming style is programmed commands sort a continuous chain of command, to the computer to execute sequential steps. Here the focus is on “how”. Now, let’s do this, then do that … A form of “hand-held”.
Unlike the imperative programming model, the declarative programming model is roughly understood as our brothers tell the computer what we want and then the computer will find a way to do it for us.
You can imagine that with Imperative Programming , you are interested in how ( how ) to solve the problem and Declarative Programming cares more about what the result will produce ( what ). Specifically here are the input ( input ) and output ( output ) of the problem.
The above two model groups are quite sectarian and FP belongs to the Declarative Programming group. OOP belongs to the Imperative Programming group
Branch of FP
Functional Programming (Functional programming) can be regarded as a branch of the declarative programming, in functional programming, we also give problems for computer resolve (something), but also give the solution ( how ), the only thing is that we never do anything to change the value / state of the variables.
I knew Functional Programming when I was working on a project with some friends at Da Nang Polytechnic School. Right 1 month after the end of the project, I officially learned in depth about Functional Programming . So on the way to discover the FP school, I met many monks who followed the same path and devoted much praise to it, motivating the curiosity and the desire to discover.
But readers should note, Functional Programming is just another cultivation path. I find that understanding FP is one of the important developments in my programming career and also a difficult one. But my curiosity, my desire to explore it, was driving me to become interested in functional programming (FP) before coming to FRP.
It seems the dominance of object-oriented programming could end. Merging models is becoming popular, even necessary. While writing code, it is completely possible to combine many different programming mechanisms, as long as True – Fast – Stable – Beautiful results are achieved.
And the Kotlin language that I will introduce here is a typical example. On this point, Hadi Hariri gave a presentation at GOTO 2018.
Or the Video Functional Programming with Kotlin by JetBrainsTV
Anyway, since then, the trend of functional thinking has spread gradually, affecting the design of many modern programs. With a little bit of attention, we can see the outstanding features of Functional Programming popping up in most frameworks and new language updates. Even if you look closely, concepts that seem irrelevant at first glance like WebComponent , Serverless , Microservice … also hide the thought of FP.
The rise of Declarative Programming
Why must know the Declarative Programming or join the FP branch? It is not recently introduced in recent years, but it has been around for a long time, from the 50s – the early days of modern computers, first with the Lisp programming language. How long it still exists in parallel with the Imperative that we do not know.
In the history of technology, it seems that OOP has prevailed over FP . You just try to look around and know, from the time you started coding, heaven has been filled with OOP rules. The job descriptions and interviews all mention OOP as basic magic. Technical discussion mostly revolves around the concepts Class , Object , Inheritance , then above is SOLID , Polymorphism , Encapsulation …
But, in the world, from Lisp to FP , then Haskell , Elixir , there has been no shortage of fake believers following the Functional Programming path. Especially after 2010, it is not clear why people began to complain more about OOP , citing more than Joe Armstrong ‘s banana / gorilla argument (2) , in which the topic of FP began to heat up again.
Functional Programming is a programming method that takes a function as the basic operating unit.
Right. FP is ideally only function, function and function. No assignment statements , no variables , no global state storage. In FP, we control program flow by combining functions together. We juggle functions back and forth, get in the function, release the function, nest it, chain it, transform them in every conceivable way.
The author believes pagan religious as you want to start the path FP need to grasp the basic concepts like immutability, Purity, Higher-order functions, Currying function, Function Composition … After Chemical nowhere realm promoted an You can learn Monad , Functor , Setoid , Idempotent , Lens … and many more.
We will not compare or highlight its pros and cons because there are quite a few articles already written about it. We will focus on practical understanding of it in the Kotlin language. Now, I would like to pause the article here, we rest here for a little while then the following article will conduct the first practice, initiation.