/*I followed instructions at
http://www.cs.yorku.ca/course_archive/1998-99/F/3401/lectures/340198-10-26HTML/
*/
/*Part one: Preparations
makeHouses/2 generates a list containing N houses,
having attributes for the color of the house,
nationality of the habitant, his pet,drink ang cigarettes.
*/
makeHouses(0,[]).
makeHouses(N,[house(Color,Nat,Pet,Drink,Cig)|List]):-N > 0, N1 is N -1,makeHouses(N1,List).
%on/2 is declared as infix operator and is true if X is on the list Xs.
:-op(100,xfy,on).
X on [X|Xs].
X on [_|Xs]:-X on Xs.
%sublist/2 for the nextTo and leftTo
sublist([S1,S2],[S1,S2|L]).
sublist(S,[_|T]):-sublist(S,T).
nextTo(H1,H2,L):-sublist([H1,H2],L).
nextTo(H1,H2,L):-sublist([H2,H1],L).
leftTo(G,W,L):-sublist([G,W],L).
/*Part two: Solving the puzzle*/
/*solve1/0 shows the answer*/
solve:-solve(FishOwner),nl,write('The '),write(FishOwner),write(' owns the fish.'),nl.
/*solve/1 FishOwner is the nationality of fishowner*/
solve(FishOwner):-makeHouses(5,List),/*generates a list, whinch is constrained by following clues*/
/*The Norwegian lives in the first house.
The Norwegian lives next to the blue house.
The man living in the center house drinks milk. (These are put in the first place, so there's not so much need to rearrange the order of houses in latter constraints)*/ List=[house(_,norwegian,_,_,_),house(blue,_,_,_,_),house(_,_,_,milk,_),_,_], /*The man who smokes Blends lives next to the one who keeps cats:*/
nextTo(house(_,_,_,_,blends),
house(_,_,cats,_,_), List),
/*The man who keeps the horse lives next to the man who smokes Dunhill*/ nextTo(house(_,_,horse,_,_),
house(_,_,_,_,dunhill),List),
/*The green house's owner drinks coffee and
the green house is on the left of the white house:*/
leftTo(house(green,_,_,coffee,_),house(white,_,_,_,_),List),
/*The man who smokes Blends has a neighbor who drinks water:*/
nextTo(house(_,_,_,_,blends),house(_,_,_,water,_),List),
/*The Brit lives in the red house:*/
house(red,brit,_,_,_) on List,
/*The Swede keeps dogs as pets:*/
house(_,swede,dogs,_,_) on List,
/*The Dane drinks tea. */
house(_,dane,_,tea,_) on List,
/*The person who smokes Pall Mall rears birds:*/
house(_,_,birds,_,pallmall) on List,
/*The owner of the yellow house smokes Dunhill:*/
house(yellow,_,_,_,dunhill) on List,
/*The owner who smokes Bluemasters drinks beer:*/
house(_,_,_,beer,bluemasters) on List,
/*The German smokes Prince:*/
house(_,german,_,_,prince) on List,
/*"FishOwner" is bound to the nationality of the fish owner*/
house(_,FishOwner,fish,_,_) on List.
|