fork download
  1. select([A|As],S):- select(A,S,S1),select(As,S1).
  2. select([],_).
  3.  
  4. dinesmans(X) :-
  5. %% Baker, Cooper, Fletcher, Miller, and Smith live on different floors
  6. %% of an apartment house that contains only five floors.
  7. select([Baker,Cooper,Fletcher,Miller,Smith],[1,2,3,4,5]),
  8.  
  9. %% Baker does not live on the top floor.
  10. Baker =\= 5,
  11.  
  12. %% Cooper does not live on the bottom floor.
  13. Cooper =\= 1,
  14.  
  15. %% Fletcher does not live on either the top or the bottom floor.
  16. Fletcher =\= 1, Fletcher =\= 5,
  17.  
  18. %% Miller lives on a higher floor than does Cooper.
  19. Miller > Cooper,
  20.  
  21. %% Smith does not live on a floor adjacent to Fletcher's.
  22. 1 =\= abs(Smith - Fletcher),
  23.  
  24. %% Fletcher does not live on a floor adjacent to Cooper's.
  25. 1 =\= abs(Fletcher - Cooper),
  26.  
  27. %% Where does everyone live?
  28. X = ['Baker'(Baker), 'Cooper'(Cooper), 'Fletcher'(Fletcher),
  29. 'Miller'(Miller), 'Smith'(Smith)].
  30.  
  31. main :- bagof( X, dinesmans(X), L )
  32. -> maplist( writeln, L), nl, write('No more solutions.')
  33. ; write('No solutions.').
Success #stdin #stdout 0.03s 6204KB
stdin
main.
stdout
[Baker(3), Cooper(2), Fletcher(4), Miller(5), Smith(1)]

No more solutions.