Advertisement
logicmoo

Untitled

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