Navigation  without Java Scripts

The Zebra-program

Another example of Prolog's general purpose problem solving capabilities 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 Englishman lives in the red house
The Spaniard owns the dog
The Norwegian lives in the first house on the lleft
Kools are smoked in the yellow house.
The man who smokes Chesterfields lives in the house
next to the man with the fox.
The Norwegian lives next to the blue house
The Winston smoker owns snails.
The lucky strike smoker drinks orange juice
The Ukrainian drinks tea
The Japanese smokes parliaments
Kools are smoked in the house next to the house where the horse is kept.
Coffee is drunk in the green house
The green house is immediately to the right (your right) of the ivory house
Milk is drunk in the middle house.

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

ZEBRA.PRJ (Use 16bit VDE)