Commits

Anonymous committed a0536cc

zebras in prolog

Comments (0)

Files changed (1)

+%% -*- Prolog -*-
+%% The Zebra puzzle
+
+%% There are five houses.
+%% The Englishman lives in the red house.
+%% The Spaniard owns the dog.
+%% Coffee is drunk in the green house.
+%% The Ukrainian drinks tea.
+%% The green house is immediately to the right of the ivory house.
+%% The Old Gold smoker owns snails.
+%% Kools are smoked in the yellow house.
+%% Milk is drunk in the middle house.
+%% The Norwegian lives in the first house.
+%% The man who smokes Chesterfields lives in the house next to the man with the fox.
+%% Kools are smoked in the house next to the house where the horse is kept. (should be "... in a house ...", see Discussion section)
+%% The Lucky Strike smoker drinks orange juice.
+%% The Japanese smokes Parliaments.
+%% The Norwegian lives next to the blue house.
+
+%% Now, who drinks water, and who owns the zebra?
+
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+% C - color of house
+% N - nationality of resident
+% P - resident's pet
+% D - resident's drink
+% S - resident's cigarette
+
+% house(C,N,P,D,S)
+
+next_to(X, Y, List) :- is_right(X, Y, List).
+next_to(X, Y, List) :- is_right(Y, X, List).
+
+is_right(L, R, [L | [R | _]]).
+is_right(L, R, [_ | Rest]) :- is_right(L, R, Rest).
+
+owns_zebra(Street, Who) :-
+	Street = [_House1, _House2, _House3, _House4, _House5],
+	member(house(red, englishman, _, _, _), Street),
+	member(house(_, spaniard, dog, _, _), Street),
+	member(house(green, _, _, coffee, _), Street),
+	member(house(_, ukrainian, _, tea, _), Street),
+	is_right(house(green,_,_,_,_), house(ivory,_,_,_,_), Street),
+	member(house(_,_, snails, _, old_gold), Street),
+	member(house(yellow, _,_,_, kools), Street),
+	[_, _, house(_, _, _, milk, _), _, _] = Street,
+	[house(_, norwegian, _, _, _) | _] = Street,
+	next_to(house(_, _, _, _, chesterfields), house(_, _, fox, _, _), Street),
+	next_to(house(_, _, _, _, kools), house(_, _, horse, _, _), Street),
+	member(house(_, _, _, orange_juice, lucky_strikes), Street),
+	member(house(_, japanese, _, _, parliaments), Street),
+	next_to(house(_, norwegian, _, _, _), house(blue, _, _, _, _), Street),
+	member(house(_, Who, zebra, _, _), Street).
+
+%% Solve by running ?- owns_zebra(Street, Who).