Advertisement
logicmoo

uses member_/2 (the invers aregs)

Nov 23rd, 2015
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 2.31 KB | None | 0 0
  1. % assume member_/2 is defined
  2.  
  3. solve(Neighborhood) :-
  4.  
  5.   /*
  6.   the solution consists of five compounds
  7.   each compound consists of the six traits of a house, in order
  8.  
  9.   position    = 1, 2, 3, 4, 5
  10.   nationality = brit, dane, german, norwegian, swede
  11.   colors      = blue, green, red, white, yellow
  12.   beverages   = beer, coffee, milk, tea, water
  13.   smokes      = bluemaster, dunhill, pallmall, prince, blend
  14.   keeps       = cat, bird, dog, fish, horse
  15.   */
  16.  
  17.   % the neighborhood looks like this
  18.   Neighborhood = [
  19.     (1,_,_,_,_,_),
  20.     (2,_,_,_,_,_),
  21.     (3,_,_,_,_,_),
  22.     (4,_,_,_,_,_),
  23.     (5,_,_,_,_,_)],
  24.  
  25.  maplist(lists:member_(Neighborhood),[
  26.   % 1.  The Brit lives in a red house.
  27.    (_,brit,red,_,_,_),
  28.  
  29.   % 2.  The Swede keeps dogs as pets.
  30.    (_,swede,_,_,_,dog),
  31.  
  32.   % 3.  The Dane drinks tea.
  33.    (_,dane,_,tea,_,_),
  34.  
  35.   % 4.  The green house is on the left of the white house (next to it).
  36.    (A,_,green,_,_), B is A+1,
  37.    (B,_,white,_,_),
  38.  
  39.   % 5.  The green house owner drinks coffee.
  40.    (_,_,green,coffee,_,_),
  41.  
  42.   % 6.  The person who smokes Pall Mall rears birds.
  43.    (_,_,_,_,pallmall,birds),
  44.  
  45.   % 7.  The owner of the yellow house smokes Dunhill.
  46.    (_,_,yellow,_,dunhill,_),
  47.  
  48.   % 8.  The man living in the house right in the center drinks milk.
  49.    (3,_,_,milk,_,_),
  50.  
  51.   % 9.  The Norwegian lives in the first house.
  52.    (1,norwegian,_,_,_,_),
  53.  
  54.   % 10. The man who smokes blend lives next to the one who keeps cats.
  55.    (C,_,_,_,blend,_), plus_or_minus_one(C, D),
  56.    (D,_,_,_,_,cats),
  57.  
  58.   % 11. The man who keeps horses lives next to the man who smokes Dunhill.
  59.    (E,_,_,_,_,horses), plus_or_minus_one(E, F),
  60.    (F,_,_,_,dunhill,_),
  61.  
  62.   % 12. The owner who smokes Blue Master drinks beer.
  63.    (_,_,_,beer,bluemaster,_),
  64.  
  65.   % 13. The German smokes Prince.
  66.    (_,german,_,_,prince,_),
  67.  
  68.   % 14. The Norwegian lives next to the blue house.
  69.    (G,norwegian,_,_,_,_), plus_or_minus_one(G, H),
  70.    (H,_,blue,_,_,_),
  71.  
  72.   % 15. The man who smokes blend has a neighbor who drinks water.
  73.    (I,_,_,_,blend,_), plus_or_minus_one(I, J),
  74.    (J,_,_,water,_,_),
  75.  
  76.   % Question: Who owns the fish?
  77.    (_,_,_,_,_,fish)]).
  78.  
  79. % This is probably very very very bad practice
  80. plus_or_minus_one(I, J) :- Im is I-1, Ip is I+1, member(J, [Im, Ip]).
  81.  
  82. main :- solve(Neighborhood), maplist(writeln, Neighborhood).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement