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

No more solutions.