Clear questions and runnable code
get the best and fastest answer
Perl and Prologby Zaxo (Archbishop)
|on Feb 04, 2002 at 10:11 UTC||Need Help??|
I should subtitle this "What I did on my PerlMonks Vacation". We all had to find another profitable use of time during the server move hiatus. This article is about what I did, and how Perl helped.
It is nearly a FAQ for someone in CB to ask, "What other computer language should I learn?" Plainly there is no single answer, but the one I usually give is "One that's completely different." Forth, Lisp, and Prolog are examples of what I mean: languages which can burn new pathways into the brain. While perlmonks.org was moving, I decided it was time to take my own advice and learn some Prolog.
I got a gnu implementation called gprolog. I also found an introductory book and an elementary tutorial online. Many Prolog resource sites are peppered with dead links, and the ISO working group for Prolog seems to have withered. Probably no HR clerk will line you up for untold wealth seeing Prolog on your resume. Its dollar value is in the brain pathways I mentioned.
I've barely advanced to baby talk in the language, but I'll try to convey some of its flavor. Prolog is called "rule based" or "logic programming". A program consists of a sequence of assertions, called "predicates". Here is a classic example (adapted from the tutorial):
If this immortal wisdom is loaded in an interactive session ('| ?-' is gprolog's baroque prompt):
Knowing enough from the first two assertions, Prolog was able to confirm the mortality of Socrates with a 'yes'. The previous 'yes' confirmed that socrates.pro was truly consulted (unfortunately, the other conventional extension for Prolog files is '.pl').
How does this work? The online book I cited explains, in a chapter called The Box Model of Execution. Each predicate is described as a black box with two inputs, Call and Redo, and two outputs, Fail and Exit. If a predicate fails, execution returns via redo to the previous predicate.
This seemed vague and unsatisfactory to me until I hit a trigger word -- backtracking. Light dawned: Prolog acts like Perl's regex engine! In fact it acts like a hyperextended dynamic regex engine, whose 'patterns' can be modified, combined, chopped, sliced and diced.
Perl gave me enough of a Prolog-type mental pathway to give me a flying start. Now I know for sure that studying Prolog will improve my Perl. That is a hell of a good bargain.
I'm gathering more impressions and ideas as I go. If there's interest, I'll enjoy airing them here from time to time.
Update: Minor cleanups in text. Added queries to show a more active kind of response from Prolog and confirmation that it never heard of Aristotle.