For Programmers: Free Programming Magazines  


Home > Archive > Prolog > March 2006 > Re: How to solve the following ``Liars'' puzzle in prolog?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: How to solve the following ``Liars'' puzzle in prolog?
jakob

2006-03-12, 6:58 pm

Bart Demoen wrote:
> When is your assignment due ?

I guess it's generally not a good idea to encourage them, but I only
recently finished Clocksin & Mellish myself - without really doing any
of the exercises, so I figured I'd look at this for practice.
(Hopefully correct) solution follows:

%% Five schoolgirls sat for an examination. Their parents -- so they
%% thought -- showed an undue degree of interest in the result. They
%% therefore agreed that, in writing home about the examination, each
%% girl should make one true statement and one untrue one. The
following
%% are the relevant passages from their letters:
%% * Betty: ``Kitty was second in the examination. I was only
third.''
%% * Ethel: ``You'll be glad to hear that I was on top. Joan was
second.''
%% * Joan: ``I was third, and poor old Ethel was bottom.''
%% * Kitty: ``I came out second. Mary was only fourth.''
%% * Mary: ``I was fourth. Top place was taken by Betty.''
%% What in fact was the order in which the five girls were placed?

all(_, []).
all(Pred, [X|Xs]) :-
P =.. [Pred, X],
call(P),
all(Pred, Xs).

distinct([]).
distinct([X|Xs]) :- not(member(X, Xs)), distinct(Xs).

x0r(A, B) :- A, not(B).
x0r(A, B) :- not(A), B.

%%======================================
=======================================

schoolgirl(betty).
schoolgirl(ethel).
schoolgirl(joan).
schoolgirl(kitty).
schoolgirl(mary).

betty(Snd, Trd) :- x0r(Snd = kitty, Trd = betty).
ethel(Fst, Snd) :- x0r(Fst = ethel, Snd = joan).
joan(Trd, Fith) :- x0r(Trd = joan, Fith = ethel).
kitty(Snd, Forth) :- x0r(Snd = kitty, Forth = mary).
mary(Forth, Fst) :- x0r(Forth = mary, Fst = betty).

solution([Fst, Snd, Trd, Forth, Fith]) :-
all(schoolgirl, [Fst,Snd,Trd,Forth,Fith]),
distinct([Fst,Snd,Trd,Forth,Fith]),
betty(Snd, Trd),
ethel(Fst, Snd),
joan(Trd, Fith),
kitty(Snd, Forth),
mary(Forth, Fst).

% Call:
% ?- solution([A,B,C,D,E]).

%%% EOF

Comments welcome!

-jakob

Markus Triska

2006-03-12, 9:57 pm

Hi,

jakob wrote:

>
> all(_, []).
> all(Pred, [X|Xs]) :-
> P =.. [Pred, X],
> call(P),
> all(Pred, Xs).


Consider using maplist/2, or flip arguments to allow for first-argument
indexing.

>
> distinct([]).
> distinct([X|Xs]) :- not(member(X, Xs)), distinct(Xs).


Better use (standard) \+/1 instead of not/1.


Here is a version based on constraints and meta-interpretation:


:- use_module(library(bounds)).

:- op(710, xfy, xor).
:- op(720, xfy, and).

true(A is A).
true(A and B) :-
true(A),
true(B).
true(A xor B) :-
true(A),
false(B).
true(A xor B) :-
false(A),
true(B).

false(A is B) :-
A #\= B.


solution(Sol) :-
Girls = [Betty,Ethel,Joan,Kitty,Mary],
Girls in 1..5,
all_different(Girls),
true(Kitty is 2 xor Betty is 3 and
Ethel is 1 xor Joan is 2 and
Joan is 3 xor Ethel is 5 and
Kitty is 2 xor Mary is 4 and
Mary is 4 xor Betty is 1),
keysort([Betty-betty,Mary-mary,Joan-joan,
Kitty-kitty,Ethel-ethel], Sol).


?- solution(Ss).

Ss = [1-kitty, 2-joan, 3-betty, 4-mary, 5-ethel]


All the best,
Markus.
Markus Triska

2006-03-13, 6:59 pm

Hi,

jakob wrote:

> Also, in case anyone else is wondering, in SWI Prolog I had to use:


No changes should be necessary for recent versions (>= 5.5.40) of SWI
Prolog.

All the best,
Markus.
Peter

2006-03-27, 4:00 am

jakob wrote:
> Thanks for the reply.
> Also, in case anyone else is wondering, in SWI Prolog I had to use:
>
> :- use_module(library('clp/bounds')).
>
> instead of
>
> :- use_module(library(bounds)).
>
> -jakob


Is there an ECLiPSe library that can be used here?

Thanks.
Joachim Schimpf

2006-03-28, 7:01 pm

Peter wrote:
> jakob wrote:
>
> Is there an ECLiPSe library that can be used here?


Sure. Use :- lib(ic), replace ... in ... by ... :: ...,
and all_different/1 by alldifferent/1.

-- Joachim
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com