Skip to main content
MyWebForum

Back to all posts

How to Write Prolog Rule?

Published on
3 min read
How to Write Prolog Rule? image

Best Prolog Programming Guides to Buy in January 2026

1 Programming in Prolog: Using The Iso Standard

Programming in Prolog: Using The Iso Standard

  • AFFORDABLE PRICES FOR QUALITY READS-GREAT FOR BUDGET-SAVVY BUYERS!
  • ECO-FRIENDLY CHOICE: REDUCE WASTE BY CHOOSING PRE-LOVED BOOKS!
  • RICH CONTENT AWAITS-INSPECTED FOR GOOD CONDITION, READY TO ENJOY!
BUY & SAVE
$67.04 $74.99
Save 11%
Programming in Prolog: Using The Iso Standard
2 The Craft of Prolog (Logic Programming)

The Craft of Prolog (Logic Programming)

BUY & SAVE
$50.00
The Craft of Prolog (Logic Programming)
3 Logic Programming with Prolog

Logic Programming with Prolog

BUY & SAVE
$44.92 $49.99
Save 10%
Logic Programming with Prolog
4 Adventure in Prolog

Adventure in Prolog

BUY & SAVE
$14.00
Adventure in Prolog
5 Expert Systems in Prolog

Expert Systems in Prolog

BUY & SAVE
$14.00
Expert Systems in Prolog
6 The Art of Prolog: Advanced Programming Techniques (Mit Press Series in Logic Programming)

The Art of Prolog: Advanced Programming Techniques (Mit Press Series in Logic Programming)

BUY & SAVE
$122.06
The Art of Prolog: Advanced Programming Techniques (Mit Press Series in Logic Programming)
7 Clause and Effect: Prolog Programming for the Working Programmer

Clause and Effect: Prolog Programming for the Working Programmer

  • QUALITY ASSURANCE: EACH BOOK IS INSPECTED FOR GOOD CONDITION.
  • AFFORDABLE PRICES: SAVE MONEY WITHOUT SACRIFICING QUALITY.
  • ECO-FRIENDLY CHOICE: CHOOSE SUSTAINABILITY BY BUYING USED BOOKS.
BUY & SAVE
$80.06 $84.99
Save 6%
Clause and Effect: Prolog Programming for the Working Programmer
8 Prolog Programming for Artificial Intelligence

Prolog Programming for Artificial Intelligence

BUY & SAVE
$101.13
Prolog Programming for Artificial Intelligence
9 Learn Prolog Now! (Texts in Computing, Vol. 7)

Learn Prolog Now! (Texts in Computing, Vol. 7)

  • HIGH-QUALITY USED BOOKS AT AFFORDABLE PRICES.
  • ECO-FRIENDLY CHOICE: REDUCE WASTE WITH USED BOOKS.
  • SATISFACTION GUARANTEE-SHOP WITH CONFIDENCE TODAY!
BUY & SAVE
$23.79 $31.00
Save 23%
Learn Prolog Now! (Texts in Computing, Vol. 7)
10 Prolog Programming in Depth

Prolog Programming in Depth

  • QUALITY ASSURANCE: THOROUGHLY CHECKED FOR READABILITY AND QUALITY.
  • COST-EFFECTIVE: AFFORDABLE PRICES FOR QUALITY USED BOOKS.
  • ECO-FRIENDLY CHOICE: SUPPORTS RECYCLING AND SUSTAINABILITY EFFORTS.
BUY & SAVE
$310.84
Prolog Programming in Depth
+
ONE MORE?

In order to write a Prolog rule, you need to define the rule using a predicate. A Prolog rule consists of a head and a body, separated by a :- symbol. The head of the rule specifies the predicate that you are defining, and the body consists of one or more predicates that must be true in order for the rule to be true.

For example, if you want to define a rule that states "X is an ancestor of Y if X is the parent of Y, or if X is the parent of Z and Z is an ancestor of Y", you would write it in Prolog like this:

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

In this example, "ancestor(X,Y)" is the head of the rule, and "parent(X,Y)" and "parent(X,Z), ancestor(Z,Y)" are the predicates in the body of the rule. This rule states that X is an ancestor of Y if X is the parent of Y, or if there exists a Z such that X is the parent of Z and Z is an ancestor of Y.

To use this rule in Prolog, you would simply query the Prolog interpreter using the ancestor predicate, passing in the desired variables as arguments. Prolog will then use the rules you have defined to determine whether the query is true or false.

What is a Prolog rule for checking if a list is sorted?

Here is a Prolog rule for checking if a list is sorted in ascending order:

% Base case: an empty list is always sorted is_sorted([]).

% Recursive case: a list is sorted if the second element is greater than or equal to the first element is_sorted([_]). is_sorted([X,Y|T]) :- X =< Y, is_sorted([Y|T]).

This rule uses recursion to compare each pair of adjacent elements in the list. If the second element is greater than or equal to the first element, the rule recursively checks the rest of the list. If all pairs of elements in the list satisfy this condition, the list is considered sorted.

How to write a Prolog rule for removing the last element from a list?

To write a Prolog rule for removing the last element from a list, you can create a rule that uses recursion to iterate through the list until the second to last element is reached. Here is an example implementation:

remove_last([X], []). remove_last([X|Xs], [X|Ys]) :- remove_last(Xs, Ys).

In this implementation, the base case for the recursion is when the list only contains one element, in which case the result is an empty list. For lists with more than one element, the head of the list is added to the result list (Ys) and the remove_last rule is called recursively with the tail of the list (Xs) until the base case is reached.

You can use this rule by calling remove_last/2 with a list as the first argument and an unbound variable as the second argument, like this:

?- remove_last([1,2,3,4], Result).

This will assign Result to [1,2,3], as the last element (4) has been removed from the list.

How to write a Prolog rule for list reversal?

Here is a Prolog rule for list reversal:

reverse_list([],[]). reverse_list([Head|Tail], Reversed) :- reverse_list(Tail, TailReversed), append(TailReversed, [Head], Reversed).

This rule states that an empty list reverses to an empty list. For a non-empty list, it recursively reverses the tail of the list and then appends the head of the list to the reversed tail to get the final reversed list.