/* Solution to a version of the zebra problem, by Robert Keller. */ /* There are five houses in a row. Each house has a unique color, * and therein lives a person with a unique nationality, occupation, * pet, and dring a unique drink. The clues are: The English person lives in the red house. The Spanish person owns a dog. The green house is on the right side of the white house. The Italian drinks tea. The Norwegian lives in the first house on the left. The photographer breeds snails. The Norwegian’s house is next to the blue one. The Japanese person is a painter. The fox is in a house next to that of the physician. The diplomat lives in the yellow house. The owner of the green house drinks coffee. The violinist drinks orange juice. The horse is in a house next to that of the diplomat. Milk is drunk in the middle house. The problem is to determine who owns the zebra and who drinks water?. */ house(X, L) :- member(X, L). firstHouse(X, [X | _]). middleHouse(X, [_, _, X, _, _]). % Assume that "left of" means "immediate left of". leftof(X, Y, [X, Y | _]). leftof(X, Y, [_ | L]) :- leftof(X, Y, L). rightof(X, Y, L) :- leftof(Y, X, L). nextto(X, Y, L) :- leftof(X, Y, L). nextto(X, Y, L) :- leftof(Y, X, L). nationality(N, (N, _, _, _, _)). color(C, (_, C, _, _, _)). occupation(O, (_, _, O, _, _)). pet(P, (_, _, _, P, _)). drink(D, (_, _, _, _, D)). clues(L) :- clue14(L), clue1(L), clue2(L), clue3(L), clue4(L), clue5(L), clue6(L), clue7(L), clue8(L), clue9(L), clue10(L), clue11(L), clue12(L), clue13(L), true. clue1(L) :- nationality(english, H), color(red, H), house(H, L). clue2(L) :- nationality(spanish, H), pet(dog, H), house(H, L). clue3(L) :- color(green, G), color(white, W), rightof(G, W, L). clue4(L) :- drink(tea, H), nationality(italian, H), house(H, L). clue5(L) :- nationality(norwegian, H), firstHouse(H, L). clue6(L) :- occupation(photographer, H), pet(snails, H), house(H, L). clue7(L) :- nationality(norwegian, N), color(blue, B), nextto(N, B, L). clue8(L) :- occupation(painter, H), nationality(japanese, H), house(H, L). clue9(L) :- pet(fox, H), occupation(physician, P), nextto(H, P, L). clue10(L) :- occupation(diplomat, H), color(yellow, H), house(H, L). clue11(L) :- color(green, H), drink(coffee, H), house(H, L). clue12(L) :- occupation(violinist, H), drink(oj, H), house(H, L). clue13(L) :- pet(horse, H), occupation(diplomat, D), nextto(H, D, L). clue14(L) :- drink(milk, H), middleHouse(H, L). pprint([]). pprint([A | L]) :- write(A), nl, pprint(L). solution(Z, W, L) :- clues(L), pet(zebra, H1), nationality(Z, H1), house(H1, L), drink(water, H2), nationality(W, H2), house(H2, L). % This test will test for a solution, and also determine whether or % not the solution is unique. test :- solution(Z, W, L) -> ( solution(_, _, M), L \== M -> write('The solution is not unique.'), write('One solution is: '), nl, pprint(L), nl, write('Another solution is: '), nl, pprint(M) ; write('The solution is unique:'), nl, pprint(L), nl, write('The '), write(Z), write(' owns the zebra.'), nl, write('The '), write(W), write(' drinks water.'), nl ) ; write('There is no solution'), nl.