Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Ovid (2709)

  (email not shown publicly)
AOL IM: ovidperl (Add Buddy, Send Message)

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Journal of Ovid (2709)

Saturday August 06, 2005
06:44 PM

AI::Prolog 0.73 and a Prolog Cookbook

[ #26153 ]

Update: regrettably, I forgot to include in the manifest. 0.732 is now on its way.

AI::Prolog 0.73 is now on its way to a CPAN mirror near you.

Once again, we have a release I'm quite excited about. There's been a lot of work to clean up the output to make it easier to understand. For example, in previous versions, run the aiprolog shell and consult a file containing the following Prolog code:

append([W|X], Y, [W|Z]) :- append(X,Y,Z).

Then, in the shell, type "listing.". You should get output similar to this:

1. append/3:
   append([_0|_1],_2,[_0|_3]) :- [append(_1,_2,_3)].

That's really ugly. The /_\d+/ represents variables. Further, in a rule, the list of terms all appear on the same line for a given clause. That can make it very difficult to read. In 0.73, doing the same thing results in this:

1. append/3:
   append([], A, A).
   append([A|B], C, [A|D]) :-
        append(B, C, D).

That's much easier to read.

However, that's not the good stuff. I've now included the AI::Prolog::Cookbook. This document only has a few "recipes", but it does provide a few Prolog solutions of common problems. Here's a Prolog implementation of the quicksort algorithm:


Usage: sort(List, Sorted).

This definition depends on the partition/4 and append/3 predicates defined in this document.

sort([], []).
sort([Head|Tail], Sorted) :-
    partition(Head, Tail, LHS, RHS),
    sort(LHS, Temp1),
    sort(RHS, Temp2),
    append(Temp1, [Head|Temp2], Sorted).

Note that (currently) this will only sort numeric values.

The cookbook presents many recipes "as is". Other recipes have moderately extensive discussions about important caveats related to them. For example, there's a comparison of the "reverse accumulate" algorithm which runs in O(n) time and the "Naive reverse" algorithm which runs in O(n^2) time.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.