I had a fun ride attending a very interesting lecture this semester called Programming Paradigms. I learned about the four main paradigms that exist: imperative, object-oriented, functional and logic programming. Now, I’m sure every developer has heard about imperative, OO and functional, but to be honest I had no idea what logic programming was about. I couldn’t even name one language in the logic programming paradigm. I was intrigued, what could this paradigm I had never heard about be, what does it excel in and could it be useful for day-to-day programming problems?
Why should I learn logic programming?
The book The Pragmatic Programmer has a tip called ”Invest Regularly in Your Knowledge Portfolio”:
Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut. Additionally, learning many languages is far easier now, thanks to the wealth of freely available software on the Internet.
We are so used to and immersed in the imperative and the object-oriented paradigm that we completely forget that there are other ways of solving the same problem. Rather than simply learning a new language in a paradigm you already know, it is even more enriching learning a new paradigm. This will be a new thought pattern, a way of a seeing the same problem from a completely new angle.
So what is logic programming?
I’d like to start with a phrase that I think that best describes this new thought pattern:
Say what you want, not how you want it done.
Get ready to have your mind blown as you will no longer be directly writing how to solve a problem, but rather expressing it in terms of facts and rules. In this post you will learn how to solve the Graph Coloring problem in six lines with Prolog. Logic programming excels in scenarios where an exhaustive search is needed, as it basically builds in backtracking for your problems automatically. Continue reading