The Zebra-program and Prologs list handling capabilities.

Another very good example of Prolog's general purpose problem solving capabilities as well as its list-handling-facilities is the Zebra program.

The problem is a classical constraint-resolution problem. In 5 houses live 5 persons of different nationality, different house-colors, different favorites for cigarette brand, drinks, different pets etc. You are given a number of facts as a starting point, and a question like "Who lives in the house left of the man that has a zebra as pet".

Have a look at the program, and see how elegant prolog solution to this problem is. Everything is represented as lists. The constraints (e.g. "in the 3rd house they smoke cigars") are put into the lists.

To solve the puzzle a small toolbox of list-handling predicates is built. It is these tools that solve the problem. Notice that you have built a very general-purpose program that can easily can be adapted to similar kinds of problems.

A PROLOG program to solve the "5 houses" problem using a "verify and choose" method. A candidate solution is any of the (5!)**5 ways of distributing 5 colours, 5 drinks, 5 nationalities, 5 cigarettes, and 5 pets amongst 5 houses. By Lewis Baxter

There are five houses with five different colours, in each house lives a person of different nationality having favorite drinks, cigaretes and pets, the information is:

The question is then: Where does the zebra live, and in which house do they drink water ?