New month, new programming language to learn, the 3rd one in this series. So the repertoire so far contains:
It is again a very different choice, logic programming. Been playing with it for the last two weeks or so, and it really makes me think differently about programming and programs. Logic and complex thinking was always a favorite past-time of me (e.g. solving puzzles and such), but only now I realized that I do have a lot to practice in this area.
Prolog is also one of the older languages (feels like a “classmate” of Lisp and Fortran) so it was the first language in the series where I could actually go to a library, take out some books to learn it, and that book wasn’t hopelessly out of date (try to do the same thing with Python or Ruby…). Since these books were also written by academics and not necessarily computer scientists, their whole approach is different, in a way more curious, though probably less practical.
In the end, I think what I would like to gain is a tool that I can use to attack problems that are intractable or at least very difficult in other languages.
One of the first thing I found hard to figure out was how to actually run a program. So in a nutshell: even if most of the interaction is within an interpreter-like environment, all the basics have to be prepared in a file and loaded.
E.g. I prepare a file like this (based on Ender’s Game), call it ender.pl:
%% Genealogy of Ender's Game. % Facts male(ender). male(peter). male('john paul'). female(valentine). female(theresa). parent('john paul', ender). parent('john paul', peter). parent('john paul', valentine). parent(theresa, ender). parent(theresa, peter). parent(theresa, valentine). % Predicates father(X, Y) :- male(X), parent(X, Y). mother(X, Y) :- female(X), parent(X, Y). sibling(X, Y) :- father(F, X), mother(M, X), father(F, Y), mother(M, Y), X \= Y. brother(X, Y) :- male(X), sibling(X, Y). sister(X, Y) :- female(X), sibling(X, Y).
Then starting Prolog I can load the file with the [‘ender.pl’]. form, and ask questions like who is Ender’s sister? Who are Theresa’s kids? Who are Peter’s siblings?
?- ['ender.pl']. % ender.pl compiled 0.00 sec, 144 bytes true. ?- sister(Sis, ender). Sis = valentine ; false. ?- mother(theresa, Kid). Kid = ender ; Kid = peter ; Kid = valentine. ?- sibling(peter, X). X = ender ; X = valentine ; false.
Well, this is laughably simple, and I’m beyond this already, but it’s good for a first illustration.
By the way, it looks like 90% of elementary Prolog examples use family trees (Nordic or Greek gods, literary, real families…)
Now let’s get in there and learn some more stuff…
Tutorials & Info
- A Short Tutorial on Prolog
- Adventure in Prolog
- Logic Programing lecture and Prolog Programming (videos)
- Prolog on Wikipedia