I've wrapped up chapter four of Seven Languages in Seven Weeks by Bruce Tang. This one is on Prolog, an interesting language unlike any that I've worked with. All of the other languages I've worked with are imperative languages, whereas Prolog is a declarative one. With imperative languages you explicitly specify the steps that an application will perform, but with a declarative one, you don't have to. You merely describe the context of a problem and what you know about it, and then let Prolog infer a solution. A cool example from the book is the following code, which determines all of the possible ways to color a map of the southeastern states, using three colors and having no two states of the same color touching:
different(red, green). different(red, blue).
different(green, red). different(green, blue).
different(blue, red). different(blue, green).
coloring(Alabama, Mississippi, Georgia, Tennessee, Florida) :-
different(Mississippi, Tennessee),
different(Mississippi, Alabama),
different(Alabama, Tennessee),
different(Alabama, Mississippi),
different(Alabama, Florida),
different(Georgia, Florida),
different(Georgia, Tennessee).
This example shows Prolog's power. With just a few lines of code, it can solve some complex problems. It's not a general purpose language, but it's a great language for constraint-based problems like airline scheduling and natural-language processing, as well as artificial intelligence. I also once found Prolog to be the easiest way to solve a problem.
I really liked learning Prolog, so much that I bought a book on it. I continue to be blown away at how easily Prolog can solve some problems - "I know this and this, now computer, tell me the answer!" That's oversimplifying it, but it captures the spirit. It's just fun to look at things in a different way. While I'm curious whether I'll ever come across a reason to use it in the workplace, I have no doubt that learning more about Prolog will be useful, as it will give me a stronger theoretical background and another perspective to consider when approaching a problem.
My answers are on GitHub. Now on to chapter five, focusing on Scala.
0 comments:
Post a Comment