Guest User

Untitled

a guest
Jan 21st, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 1.45 KB | None | 0 0
  1. %There are 8 candidates, A, B, C, D, E, F, G, H, for an expedition to the North Pole. Only 6 candidates have to be selected: one biologist, hydrologist, weather forecaster, telegraphist, mechanician and doctor.
  2. %E and G can carry duties of a biologist,
  3. %B and F - a hydrologist,
  4. %F and G - a weather forecaster,
  5. %C and D - a telegraphist,
  6. %C and H - a mechanician, and
  7. %A and D - a doctor.
  8. %However, the candidates chosen can not share the duties.
  9. %Find possible candidate groups, if
  10.  
  11. %F can only go together with B,
  12. %D - with H and C,
  13. %C can not go with G, and
  14. %A not with B.
  15.  
  16. group(A,B,C,D,E,F) :-
  17.     bio(A),
  18.     hydro(B),
  19.     weath(C),
  20.     tel(D),
  21.     mech(E),
  22.     doc(F).
  23.     no_doubles([A,B,C,D,E,F]).
  24.     rules(G),
  25.  
  26. bio(e).
  27. bio(g).
  28. hydro(b).
  29. hydro(f).
  30. weath(f).
  31. weath(g).
  32. tel(c).
  33. tel(d).
  34. mech(c).
  35. mech(h).
  36. doc(a).
  37. doc(d).
  38.  
  39. no_doubles([]).
  40. no_doubles([A|B]) :-
  41.     not(contains(A,B)),
  42.     no_doubles(B).
  43.  
  44. takeout(A,[A|B],B).
  45. takeout(A,[B|C],[B|D]) :-
  46.     takeout(A,C,D).
  47.  
  48. contains(A,[A|_]).
  49. contains(_,[]) :- false.
  50. contains(A,[_|C]) :-
  51.     contains(A,C).
  52.  
  53. rules(contains(f,G)) :-
  54.     contains(b,G),
  55.     takeout(f,G,NG),
  56.     rules(NG).
  57. rules(contains(d,G)) :-
  58.     contains(h,G),
  59.     contains(c,G),
  60.     takeout(d,G,NG),
  61.     rules(NG).
  62. rules(contains(c,G)) :-
  63.     not(contains(g,G)),
  64.     takeout(c,G,NG),
  65.     rules(NG).
  66. rules(contains(a,G)) :-
  67.     not(contains(b,G)),
  68.     takeout(a,G,NG),
  69.     rules(NG).
  70. rules(_).
Add Comment
Please, Sign In to add comment