On occasion I start working on something but then forget the reason I got started in the first place. This time at RC, I find it helpful to go back to SICP to answer the questions like “why learn Prolog?”.
Baker, Cooper, Fletcher, Miller, and Smith live on different floors of an apartment house that contains only five floors. Baker does not live on the top floor. Cooper does not live on the bottom floor. Fletcher does not live on either the top or the bottom floor. Miller lives on a higher floor than does Cooper, Smith does not live on a floor adjacent to Fletcher’s. Fletcher does not live on a floor adjacent to Cooper’s. Where does everyone live?
select([A|As],S) :- select(A,S,S1), select(As,S1).
select([],_).
dinesmans(X) :-
%% Baker, Cooper, Fletcher, Miller, and Smith on different floors
%% of an apartment house with five floors.
select([Baker,Cooper,Fletcher,Miller,Smith],[1,2,3,4,5]),
%% Baker does not live on the top floor.
Baker =\= 5,
%% Cooper does not live on the bottom floor.
Cooper =\= 1,
%% Fletcher does not live on either the top or the bottom floor.
Fletcher =\= 1, Fletcher =\= 5,
%% Miller lives on a higher floor than does Cooper.
Miller > Cooper,
%% Smith does not live on a floor adjacent to Fletcher's.
1 =\= abs(Smith - Fletcher),
%% Fletcher does not live on a floor adjacent to Cooper's.
1 =\= abs(Fletcher - Cooper),
%% Where does everyone live?
X = ['Baker'(Baker), 'Cooper'(Cooper), 'Fletcher'(Fletcher),
'Miller'(Miller), 'Smith'(Smith)].
main :- bagof( X, dinesmans(X), L )
-> maplist( writeln, L), nl, write('No more solutions.')
; write('No solutions.’).
Verse and Mercury are functional logic languages; Mercury even calls itself 'Prolog meets Haskell’. Details will follow in later posts.