/* ********************************************************************** * * CLP(R) Version 2.0 (Example Programs Release) * (C) Copyright, March 1986, Monash University * ********************************************************************** */ % % A program to solve the Zebra Puzzle. % /* Each nested s in the first arg to houses forces another house to be added to List. This effectively builds up a list of all 5 houses, where each house is a predicate 'house' with 5 arguments. Once all the s's have been stripped off, List is now a list of 5 houses. We now check that each constraint is met. The constraints that say member(house(...), List) check that a house on the list fulfills the constraint. The constraint that says sublist([house(...),house(...)],List) check that the first house is to the left of the second. The constraints that say nextto(h1,h2,list) check that h1 is either to the left of h2 or vice versa. The constraint eq([H1,H2,house(..., milk, ...),H4,H5],List) forces the milk house to be in the middle. Similarly, the other eq constraint forces craig's house to be on the left. */ zebra(Zebraowner,Drinkswater) :- houses(s(s(s(s(s(zero))))), List), member(house( red, jeremy, _, _, _) ,List), member(house( _, molly, dog, _, _) ,List), member(house(green, _, _, coffee, _) ,List), member(house( _, brian, _, tea, _) ,List), sublist([house(ivory, _, _, _, _) , house(green, _, _, _, _)],List), member(house( _, _, snail, _, snickers),List), member(house(yellow, _, _, _, jrmints),List), eq([H1,H2,house( _, _, _, milk, _),H4,H5], List), eq([house( _, craig, _, _, _)|Hrest], List), nextto(house( _, _, _, _, skittles), house( _, _, fox, _, _),List), nextto(house( _, _, _, _, jrmints), house( _, _, horse, _, _),List), member(house( _, _, _, oj, sweettarts),List), member(house( _, todd, _, _, mandms),List), nextto(house( _, craig, _, _, _), house( blue, _, _, _, _),List), member(house( _, Drinkswater, _, water, _),List), member(house( _, Zebraowner, zebra, _, _),List). eq(X, X). houses(zero, []). houses(s(N), [house(Color,Student,Pet,Drink,Snack)|List]) :- houses(N, List). member(X, [X|R]). member(X, [Y|R]) :- member(X, R). sublist(S, L) :- append(S, S2, L). sublist(S, [H|T]) :- sublist(S, T). append([], L, L). append([X|R], Y, [X|T]) :- append(R, Y, T). nextto(H1, H2, L) :- sublist([H1, H2], L). nextto(H1, H2, L) :- sublist([H2, H1], L). go :- zebra(Zebraowner, Drinkwater), printf("Zebraowner = %, Drinkwater = %\n", [Zebraowner, Drinkwater]). % Answer: % Zebraowner = japanese, Drinkwater = norwegian ?- printf("\n>>> Sample goal: go/0\n", []).