For Programmers: Free Programming Magazines  


Home > Archive > Prolog > December 2004 > I need Help with Prolog puzzle ..









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 I need Help with Prolog puzzle ..
cybershaman

2004-12-09, 3:56 am

Hello,
I have this kinda easy-to-figure-out-by-hand problem, but the major
headache is that this problem has to be solved in Prolog, I was
wondering if any you guys know how to do so..

Here is the problem:

David, Debbie, Sandra, and Alan all go to the same school, but they are
in different classes. Their teachers' names are Stone, Rock, Land,
and Waters. Their favorite subjects are math, reading, gym, and
science. Use the clues below to match each student with his or her
favorite subject and teacher.

1. The girl who likes math and the boy who likes science are not in
Water's class
2. Sandra, Alan and the girl in Stone's class don't like gym
3. The girl in Land's class likes reading

Rick van Krevelen

2004-12-10, 8:59 am

Try this:
=============
% Initialize data

student(david).
student(debbie).
student(sandra).
student(alan).
subject(math).
subject(reading).
subject(gym).
subject(science).
teacher(stone).
teacher(rock).
teacher(land).
teacher(waters).

girl(debbie).
girl(sandra).
boy(david).
boy(alan).

favorite(Student, Subject, Teacher) :-
student(Student),
subject(Subject),
teacher(Teacher).

% Clue 1
favorite(Student, math, Teacher) :-
girl(Student),
Teacher \= water.
favorite(Student, math, Teacher) :-
boy(Student),
Teacher \= water.

% Clue 2
favorite(sandra, Subject, _Teacher) :-
Subject \= gym.
favorite(alan, Subject, _Teacher) :-
Subject \= gym.
favorite(Student, Subject, stone) :-
girl(Student),
Subject \= gym.

% Clue 3
favorite(Student, reading, land) :-
girl(Student).

solution :-
favorite(X1, Y1, Z1),
favorite(X2, Y2, Z2),
favorite(X3, Y3, Z3),
favorite(X4, Y4, Z4),
X1 \= X2, X1 \= X3, X1 \= X4, X2 \= X3, X2 \= X4, X3 \= X4,
Y1 \= Y2, Y1 \= Y3, Y1 \= Y4, Y2 \= Y3, Y2 \= Y4, Y3 \= Y4,
Z1 \= Z2, Z1 \= Z3, Z1 \= Z4, Z2 \= Z3, Z2 \= Z4, Z3 \= Z4,
write(X1), write(' likes '), write(Y1), write(' and '), write(Z1), nl,
write(X2), write(' likes '), write(Y2), write(' and '), write(Z2), nl,
write(X3), write(' likes '), write(Y3), write(' and '), write(Z3), nl,
write(X4), write(' likes '), write(Y4), write(' and '), write(Z4), nl.

========================================
===
After consulting these clauses, type 'solution.' to get prolog compute the
answer

> Hello,
> I have this kinda easy-to-figure-out-by-hand problem, but the major
> headache is that this problem has to be solved in Prolog, I was
> wondering if any you guys know how to do so..
>
> Here is the problem:
>
> David, Debbie, Sandra, and Alan all go to the same school, but they are
> in different classes. Their teachers' names are Stone, Rock, Land,
> and Waters. Their favorite subjects are math, reading, gym, and
> science. Use the clues below to match each student with his or her
> favorite subject and teacher.
>
> 1. The girl who likes math and the boy who likes science are not in
> Water's class
> 2. Sandra, Alan and the girl in Stone's class don't like gym
> 3. The girl in Land's class likes reading
>



A.L.

2004-12-10, 3:58 pm

On Fri, 10 Dec 2004 10:47:31 GMT, "Rick van Krevelen"
<krevelen@cs.vu.nl> wrote:

>Try this:
>=============
>% Initialize data
>
>student(david).
>
> write(X3), write(' likes '), write(Y3), write(' and '), write(Z3), nl,
> write(X4), write(' likes '), write(Y4), write(' and '), write(Z4), nl.
>
> ========================================
===
>After consulting these clauses, type 'solution.' to get prolog compute the
>answer


Thanks for making his homework. Who will get the grade?...

A.L.
cybershaman

2004-12-10, 3:58 pm


A.L. wrote:
> On Fri, 10 Dec 2004 10:47:31 GMT, "Rick van Krevelen"
> <krevelen@cs.vu.nl> wrote:
>
nl,[color=darkred]
nl.[color=darkred]
compute the[color=darkred]
>
> Thanks for making his homework. Who will get the grade?...
>
> A.L.


Wow, someone actually replied..
Sorry, guys, but I forgot to post the answer that I have came up
with.., so noone will think bad about me :)
Especially Dr.Chiang..
Here we go:

% *** First we define all the people*********************
% We define the names of the girls
girl(debbie). girl(sandra). boy(david). boy(alan).

% We define the names of the boys
teacher(land). teacher(stone). teacher(rock). teacher(waters).

%Students in class can be either girl or boy
student(Z) :- girl(Z). student(Z) :- boy(Z).


% *** Now we define who likes what subject *************
%FROM:
% the girl who likes math
like(X, math) :-
girl(X),
not(like(X, reading)).
%FROM:
% the boy who likes science
% (and dont like anything else, f.e., gym,
% to differ two boys, since girls in the puzzle
% are into reading and math)
like(X, science) :-
boy(X),
not(like(X, gym)).
%FROM:
% Sandra, Alan, and the girl in Stone's
% class don't like gym
like(X, gym) :-
student(X),
not(X=sandra),
not(X=alan),
not(taught_by(X, stone)).
%FROM:
% the girl in Land's class likes reading
%(and don't like anything else)
like(X, reading) :-
taught_by(X, land).

% *** Now we define whom each teacher teaches ************
%FROM:
% The girl who likes math and the boy who likes
% science are NOT in Water's class
% ...therefore in Water's class are students who
% don't like math and science
%ALSO FROM:
%The girl in Land's class likes reading
% ...therefore, the other girl is in Land's class
taught_by(X, waters) :-
student(X),
not(like(X, math)),
not(like(X, science)),
not(taught_by(X, land)).

% FROM:
% .. the (other then Sandra) girl
% in Stone's class don't like gym
taught_by(X, stone) :-
girl(X),
not(X=sandra).

%FROM
% The girl in Land's class (therefore
% not in Stone's class obviously)
taught_by(X, land) :-
girl(X),
not(taught_by(X, stone)).
%FROM:
% since there is nothing about Rock's class
% in Rock's class are students who are not
% taught by anyone else but Rock
taught_by(X, rock) :-
student(X),
not(taught_by(X, waters)),
not(taught_by(X, land)),
not(taught_by(X, stone)).


%*** Now we match each teacher with each student and class **********
match(Student, Class, Teacher) :-
like(Student, Class),
taught_by(Student, Teacher).


%*** Print the answer ****************************************
***
?- match(X, Y, Z),
nl, write("Student: "), write(X), nl,
write("Class: "), write(Y), nl,
write("Teacher: "), write(Z), nl.


Student: debbie
Class: math
Teacher: stone
Yes.

Student: alan
Class: science
Teacher: rock
Yes.

Student: david
Class: gym
Teacher: waters
Yes.

Student: sandra
Class: reading
Teacher: land
Yes.
No.

Cesar Rabak

2004-12-18, 8:56 pm

Rick van Krevelen escreveu:
> Try this:

[snipped]

> ========================================
===
> After consulting these clauses, type 'solution.' to get prolog compute the
> answer
>
>

Testing with SWI-Prolog Version 5.4.2, I got:

?- solution.
david likes math and stone
debbie likes reading and rock
sandra likes gym and land
alan likes science and waters

I think the third line does not comply to the clue 2, in fact I think
the rules say no girl likes gym! OTOH, due clue 3, Sandra would like
reading, isn't it?

--
Cesar Rabak


Sponsored Links







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

Copyright 2008 codecomments.com