steamy

Prolog Project

May 3rd, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 4.15 KB | None | 0 0
  1. /*
  2. Author: Kevin Nash
  3. Date: 1 May 2015
  4. */
  5.  
  6. firstname(brenda).
  7. firstname(cally).
  8. firstname(linda).
  9. firstname(rachel).
  10. firstname(sheila).
  11.  
  12. lastname(enfield).
  13. lastname(mann).
  14. lastname(ridge).
  15. lastname(scopefield).
  16. lastname(wish).
  17.  
  18. month(february).
  19. month(april).
  20. month(may).
  21. month(july).
  22. month(september).
  23.  
  24. day(fourth).
  25. day(eighth).
  26. day(twelveth).
  27. day(seventeenth).
  28. day(twentythird).
  29.  
  30. lie(day_1).
  31. lie(day_2).
  32. lie(day_3).
  33. lie(month_1).
  34. lie(month_2).
  35.  
  36. solve :-
  37.     lastname(BrendaLast), lastname(CallyLast), lastname(LindaLast), lastname(RachelLast), lastname(SheilaLast),
  38.     all_different([BrendaLast, CallyLast, LindaLast, RachelLast, SheilaLast]),
  39.    
  40.     month(BrendaMonth), month(CallyMonth), month(LindaMonth), month(RachelMonth), month(SheilaMonth),
  41.     all_different([BrendaMonth, CallyMonth, LindaMonth, RachelMonth, SheilaMonth]),
  42.    
  43.     day(BrendaDay), day(CallyDay), day(LindaDay), day(RachelDay), day(SheilaDay),
  44.     all_different([BrendaDay, CallyDay, LindaDay, RachelDay, SheilaDay]),
  45.    
  46.     lie(BrendaLie), lie(CallyLie), lie(LindaLie), lie(RachelLie), lie(SheilaLie),
  47.     all_different([BrendaLie, CallyLie, LindaLie, RachelLie, SheilaLie]),
  48.  
  49.     Quintuples = [ [brenda, BrendaLast, BrendaMonth, BrendaDay, BrendaLie],
  50.                  [cally, CallyLast, CallyMonth, CallyDay, CallyLie],
  51.                  [linda, LindaLast, LindaMonth, LindaDay, LindaLie],
  52.                  [rachel, RachelLast, RachelMonth, RachelDay, RachelLie],
  53.                  [sheila, SheilaLast, SheilaMonth, SheilaDay, SheilaLie] ],
  54.    
  55.     % 1. Linda's last name is not Ridge.
  56.     \+ member([linda, ridge, _, _, _], Quintuples),
  57.     % 1. Linda's birthday was not in September.
  58.     \+ member([linda, _, september, _, _], Quintuples),
  59.     % 1. Linda's birthday was on the fourth day of the month.
  60.     member([linda, _, _, fourth, _], Quintuples),
  61.    
  62.     % 2. The girl born on May 12th didn't have a last name of Ridge.
  63.     \+ member([_, ridge, may, twelveth, _], Quintuples),
  64.     % 2. The girl born in February lied about the day and has a last name of Mann.
  65.     member([_, mann, february, _, day_3], Quintuples),
  66.    
  67.     % 3. The girl whose last name is Enfield had her birthday in September.
  68.     member([_, enfield, september, _, _], Quintuples),
  69.     % 3. Cally's last name is not Enfield.
  70.     \+ member([cally, enfield, _, _, _], Quintuples),
  71.    
  72.     % 4. The girl with the last name Wish lied about the day.
  73.     member([_, wish, _, _, day_1], Quintuples),
  74.     % 4. Rachel lied about the day.
  75.     member([rachel, _, _, _, day_3], Quintuples),
  76.     % 4. The girl born on the 23rd lied about the day.
  77.     member([_, _, _, twentythird, day_2], Quintuples),
  78.     % 4. The girls above are different.
  79.     \+ member([rachel, wish, _, _, _], Quintuples),
  80.     \+ member([rachel, _, _, twentythird, _], Quintuples),
  81.     \+ member([_, wish, _, twentythird, _], Quintuples),
  82.    
  83.     % 5. Sheila lied about the month.
  84.     member([sheila, _, _, _, month_2], Quintuples),
  85.     % 5. Rachel was born on the 17th.
  86.     member([rachel, _, _, seventeenth, _], Quintuples),
  87.     % 5. Rachel was not born in April.
  88.     \+ member([rachel, _, april, _, _], Quintuples),
  89.    
  90.     % 6. Brenda was born in May
  91.     member([brenda, _, may, _, _], Quintuples),
  92.     % 6. Brenda's last name is not Scopefield.
  93.     \+ member([brenda, scopefield, _, _, _], Quintuples),
  94.     % 6. Cally was not born on the 8th.
  95.     \+ member([cally, _, _, eighth, _], Quintuples),
  96.     % 6. Cally was born in July.
  97.     member([cally, _, july, _, _], Quintuples),
  98.    
  99.     tell(brenda, BrendaLast, BrendaMonth, BrendaDay, BrendaLie),
  100.     tell(cally, CallyLast, CallyMonth, CallyDay, CallyLie),
  101.     tell(linda, LindaLast, LindaMonth, LindaDay, LindaLie),
  102.     tell(rachel, RachelLast, RachelMonth, RachelDay, RachelLie),
  103.     tell(sheila, SheilaLast, SheilaMonth, SheilaDay, SheilaLie).
  104.  
  105. % Succeeds if all elements of the argument list are bound and different.
  106. % Fails if any elements are unbound or equal to some other element.
  107. all_different([H | T]) :- member(H, T), !, fail.
  108. all_different([_ | T]) :- all_different(T).
  109. all_different([_]).
  110.  
  111. tell(A, B, C, D, E) :-
  112.     write(A), write(' '), write(B), write(' was born on the '), write(D), write(' of '), write(C),
  113.     write(' but lied about the '), write(E), write('.'), nl.
Add Comment
Please, Sign In to add comment