Topic outline

  • Factorials

    The following program shows how Prolog can calculate factorial numbers. To run the program: you should have gprolog installed; load the program with the command consult('factorial'). including the full-stop; and finally run it with factorial(n,F)..

    factorial(0,1).
    factorial(N,F) :-
    N>0,
    N1 is N-1,
    factorial(N1,F1),
    F is N * F1.
  • Pingu

    Can Pingu fly?

    Try flies(pingu). and flies(tweetie).

    flies(X) :- abnormal(X), ! , fail.
    flies(X) :- bird(X).
    bird(pingu).
    bird(tweetie).
     
    penguin(pingu).
     
    abnormal(X):-penguin(X).
  • Relationships

    The following program finds the relationships between people.

    female(jane).
    female(anne).
    female(sandip).
     
    male(charnjit).
    male(jaz).
    male(tom).

    First Test: male(X).

    parent(jane,mary).
    parent(jane,rajinder).
    parent(charnjit,mary).
    parent(charnjit,rajinder).
    parent(sandip,atif).
    parent(jaz,atif).
    parent(mary,frank).
    parent(frank,george).

    Second Test: parent(X,atif), female(X).

    Third Test: parent(X,_),male(X).

    father(X, Y) :- parent(X, Y), male(X).

    fourth test: father(X, rajinder).

    ancestor(X, Y) :- parent(X, Y).