Article: 6229 of comp.lang.prolog Newsgroups: comp.lang.prolog Path: news.claremont.edu!ucivax!news.service.uci.edu!usc!cs.utexas.edu!torn!nott!bnrgate!bgtys9!bnr.ca!vellino From: vellino@bnr.ca (Andre Vellino) Subject: Re: Zebra exercice anyone ? Message-ID: <1993Feb4.223912.18143@bnr.ca> Sender: news@bnr.ca Organization: Bell-Northern Research References: <1kp24jINNt4q@usenet.INS.CWRU.Edu> Date: Thu, 4 Feb 93 22:39:12 GMT Lines: 68 Since you ask ... Here's the Zebras puzzle and it's solution by, I believe, Maarten van Emden. It's the simplest pure prolog solution I've ever seen and it's efficient too. /* There are five consecutive houses, each of a different color and inhabited by men of different nationalities. They each own a different pet, have a different favorite drink and drive a different car. 1. The Englishman lives in the red house. 2. The Spaniard owns the dog. 3. Coffee is drunk in the green house. 4. The Ukrainian drinks tea. 5. The green house is immediately to the right of the ivory house. 6. The Porsche driver owns snails. 7. The Masserati is driven by the man who lives in the yellow house. 8. Milk is drunk in the middle house. 9. The Norwegian lives in the first house on the left. 10. The man who drives a Saab lives in the house next to the man with the fox. 11. The Masserati is driven by the man in the house next to the house where the horse is kept. 12. The Honda driver drinks orange juice. 13. The Japanese drives a Jaguar. 14. The Norwegian lives next to the blue house. The problem is: Who owns the Zebra? Who drinks water? */ /* programming pearl by M. H. van Emden */ member(X,[X|_]). member(X,[_|Y]) :- member(X,Y). left_right(L,R,[L,R,_,_,_]). left_right(L,R,[_,L,R,_,_]). left_right(L,R,[_,_,L,R,_]). left_right(L,R,[_,_,_,L,R]). next_to(X,Y,L) :- left_right(X,Y,L). next_to(X,Y,L) :- left_right(Y,X,L). /* each house is structured like this [Color,Nationality,Car,Drink,Pet] S is the list of 5 houses */ zebra(S) :- S = [[_,norwegian,_,_,_],_,[_,_,_,milk,_],_,_], next_to([_,norwegian,_,_,_],[blue,_,_,_,_],S), member([green,_,_,coffee,_],S), left_right([ivory,_,_,_,_],[green,_,_,_,_],S), member([red,englishman,_,_,_],S), member([_,ukranian,_,tea,_],S), member([yellow,_,masserati,_,_],S), member([_,_,honda,orange_juice,_],S), member([_,japanese,jaguar,_,_],S), member([_,spaniard,_,_,dog],S), next_to([_,_,masserati,_,_],[_,_,_,_,horse],S), member([_,_,porsche,_,snails],S), next_to([_,_,saab,_,_],[_,_,_,_,fox],S). /* ?- zebra([_A,_B,_C,_D,_E]). */   Article: 6231 of comp.lang.prolog Path: news.claremont.edu!bridge2!sgiblab!swrinde!elroy.jpl.nasa.gov!usc!cs.utexas.edu!sun-barr!decwrl!waikato.ac.nz!aukuni.ac.nz!john-ha From: j_hamer@cs.aukuni.ac.nz (j_hamer) Newsgroups: comp.lang.prolog Subject: Re: Zebra exercice anyone ? Message-ID: Date: 5 Feb 93 02:11:08 GMT References: <1kp24jINNt4q@usenet.INS.CWRU.Edu> Sender: news@ccu1.aukuni.ac.nz (News Owner) Followup-To: comp.lang.prolog Organization: computer Science Lines: 110 Nntp-Posting-Host: john-ha.cs.aukuni.ac.nz In article <1kp24jINNt4q@usenet.INS.CWRU.Edu>, cs438@cleveland.Freenet.Edu (Denis LaFont) wrote: > > I am looking for the solution of the famous "zebra" problem in PROLOG. > This is an easy problem for Prolog, since the search space is entirely tractable. The only challenge is to write the code as elegantly as possible. I have used the solution below as an introductory example for a number of years (it uses two Quintus Prolog library routines: nextto/3 and perm2/4 --- their definitions are left as an exercise to any non-QP users out there...). %----CUT HERE---- /* File: zebra.pl Purpose: Solve the Zebra puzzle Author: John Hamer Department of Computer Science University of Auckland Notes: There are 5 houses, each of a different colour and inhabited by a man of a different nationality, with a different pet, drink and brand of cigarettes. 1) The Englishman lives in the red house 2) The Spaniard owns the dog 3) Coffee is drunk in the green house 4) The Ukrainian drinks tea 5) The green house is immediately to the right (your right) of the ivory house 6) The Winston smoker owns snails 7) Kools are smoked in the yellow house 8) Milk is drunk in the middle house 9) The Norwegian lives in the first house on the left 10) The man who smokes Chesterfields lives in the house next to the man with the fox 11) Kools are smoked in the house next to the house where the horse is kept 12) The Lucky Strike smoker drinks orange juice 13) The Japanese smokes Parliaments 14) the Norwegian lives next to the blue house Q. Who owns the Zebra? Who drinks water? */ :- use_module( library(basics), [ member/2 ] ). :- use_module( library(lists), [ nextto/3, % A and B are adjacent elements in List perm2/4 % perm2( A, B, A, B ). perm2( A, B, B, A ). ] ). % % A street is a list of 5 houses; each house has a colour, a nationality % a pet, a drink, and a smoke. % street([house(_,_,_,_,_), house(_,_,_,_,_), house(_,_,_,_,_), house(_,_,_,_,_), house(_,_,_,_,_)] ). % % We are told the following: % constraints( Street ) :- member(house(red, englishman, _, _, _), Street), % 1) member(house(_, spaniard, dog, _, _), Street), % 2) member(house(green, _, _, coffee, _), Street), % 3) member(house(_, ukrainian, _, tea, _), Street), % 4) nextto(house(ivory, _, _, _, _), house(green, _, _, _, _), Street), % 5) member(house(_, _, snails, _, winston), Street), % 6) member(house(yellow, _, _, _, kools), Street), % 7) Street = [_, _, house(_, _, _, milk, _), _, _], % 8) Street = [house(_, norwegian, _, _, _)|_], % 9) perm2(Left_10, Right_10, house(_, _, _, _, chesterfields), house(_, _, fox, _, _)), nextto(Left_10, Right_10, Street), % 10) perm2(Left_11, Right_11, house(_, _, _, _, kools), house(_, _, horse, _, _)), nextto(Left_11, Right_11, Street), % 11) member(house(_, _, _, orange, lucky_strike), Street), % 12) member(house(_, japanese, _, _, parliaments), Street), % 13) perm2(Left_14, Right_14, house(_, norwegian, _, _, _), house(blue, _, _, _, _)), nextto(Left_14, Right_14, Street). % 14) % % Who owns the Zebra? % zebra( Who ) :- street(Street), constraints(Street), member(house(_, Who, zebra, _, _), Street). % % Who drinks water? % water( Who ) :- street(Street), constraints(Street), member(house(_, Who, _, water, _), Street). -- John Hamer Email: J_Hamer@cs.aukuni.ac.nz -- Department of Computer Science -- University of Auckland, New Zealand.