f

#### A simple problem, but a problem ...

```Hello,

can anyone solve the following problem ?
It looks quite simple to me, but I can't get it right.

mr(yes).
mr(no).
mrs(yes).
mrs(no).
marcel(yes).
marcel(no).
jacqu(yes).
jacqu(no).
grandpa(yes).
grandpa(no).

but how can I use this to solve the problem ?

thanx a lot
Maxx

Problem:

If Mr. Johnson goes, his wife goes too.
At least one of the children, Marcel or Jacqueline, goes too.
If Marcel goes, grandpa goes too.
If Mrs. Johnson goes, grandpa stays at home.
If Jacqueline goes, Mr. Johnson and Marcel go too.

Write a prolog program, which determines, which members of the
family Johnson, given the mentioned restrictions, will go to the party.

Advice: Create a variable for each member of the family, which can have
the constant values 'yes' or 'no'.
```
 0
ia
12/5/2004 11:04:48 PM
comp.lang.prolog 3728 articles. 0 followers.

6 Replies
1691 Views

Similar Articles

[PageSpeed] 42

```Andr? I. wrote:

>
> The advice suggests the following:
>
....
> Advice: Create a variable for each member of the family, which can have
> the constant values 'yes' or 'no'.

According to the advice, you should create variables. Hint:

possibility(yes).
possibility(no).

possibilities([]).
possibilities([P|Ps]) :-
possibility(P),
possibilities(Ps)

validparty([X1,X2,X3,X4,X5]) :-
possibilities([X1,X2,X3,X4,X5]),
... put the real stuff here ...

?- validparty([Mr,Mrs,Marcel,Jacqueline,Grandpa]).

Mr = no
Mrs = no
Marcel = yes
Jac = no
Grandpa = yes

Best regards,
Markus.
```
 0
Markus
12/6/2004 12:22:49 AM
```Hello,

So far, so good. But I can't figure out, how I can express for example
this line:
"If Mr. Johnson goes, his wife goes too."
Can you give me a concret example for this line ?=20

thank you=20
Andr=E9

```
 0
ia
12/6/2004 2:25:10 PM
```Hi,

> So far, so good. But I can't figure out, how I can express for example
> this line:
> "If Mr. Johnson goes, his wife goes too."
> Can you give me a concret example for this line ?=20

Same problem here, Andr=E9. I have come this far, but I'm missing the=20
if_clause condition:

% A family is about to visit their friends. They are a bit complicated=20
as to who
% is going. The problematic part of the family consist of Mr. Funky,=20
Mrs. Funky,
% the kids Marc and Joseline and grandpa Funky. The rules are as follows:=

%
% If Mrs. Funky goes, his wife goes as well.
% At least one of the kids Marc and Joseline will be going.
% If Marc goes, grandpa Funky will be going.
% If Mrs. Funky goes, grandpa Funky will not be going.
% If Joseline goes, Mr. Funky and Marcel will be going.
%
% Who will be visiting their friends?
%
% Solution: Marc and grandpa go, the rest stays home.
%
% H: husband
% W: wife
% G: grandpa
% M: marc
% J: joseline

yes_no(yes).
yes_no(no).

goes(H,W,G,M,J):-
yes_no(H), yes_no(W),
yes_no(M), yes_no(J), (M=3Dyes; J=3Dyes),
yes_no(G),
if_clause(M=3Dyes, G=3Dyes),
if_clause(H=3Dyes, W=3Dyes),
if_clause(W=3Dyes, G=3Dnein),
if_clause(J=3Dyes, (H=3Dyes, M=3Dyes)).

Cheers,
eb
--=20
```
 0
el_bandido
12/6/2004 4:33:41 PM
```el_bandido@nospam.com wrote:

>   if_clause(M=yes, G=yes),
>   if_clause(H=yes, W=yes),
>   if_clause(W=yes, G=nein),
>   if_clause(J=yes, (H=yes, M=yes)).

implies(A,B) :-
((A,B);\+ A).

Try to come up with different solutions; for example, expressing the
third statement directly (via a specialized predicate). Also, you don't
have to use the same construct for every "if"-statement in the handout!
Especially the last one could be irritating at first and you could also
code a specialized predicate for it.

Best regards,
Markus.
```
 0
Markus
12/6/2004 6:56:12 PM
```Dear Andre (?),

"Andr? I." wrote:
>
> Hello,
>
> can anyone solve the following problem ?
> It looks quite simple to me, but I can't get it right.
>
> The advice suggests the following:
>
> mr(yes).
> mr(no).
> mrs(yes).
> mrs(no).
> marcel(yes).
> marcel(no).
> jacqu(yes).
> jacqu(no).
> grandpa(yes).
> grandpa(no).
>
> but how can I use this to solve the problem ?
>
> thanx a lot
> Maxx
>
> Problem:
>
> If Mr. Johnson goes, his wife goes too.
> At least one of the children, Marcel or Jacqueline, goes too.
> If Marcel goes, grandpa goes too.
> If Mrs. Johnson goes, grandpa stays at home.
> If Jacqueline goes, Mr. Johnson and Marcel go too.
>
> Write a prolog program, which determines, which members of the
> family Johnson, given the mentioned restrictions, will go to the party.
>
> Advice: Create a variable for each member of the family, which can have
> the constant values 'yes' or 'no'.

Remember that Prolog variables are like pronouns. Since we are
interested in who goes (yes?) and who stays at home (no?), we might have
a predicate, go(Grandpa,Father,Mother,Son,Daughter). Note that the
variables are capitalized and different arguments to the predicate.

Now we can say

go(Grandpa,Father,Mother,Son,Daughter)  :-
grandpa(Grandpa),
father(Father),
mother(Mother),
marcel(Son),
jacqueline(Daughter).

grandpa(yes).
grandpa(no).

father(yes).
father(no).

mother(yes).
mother(no).

marcel(yes).
marcel(no).

jacqueline(yes).
jacqueline(no).

This may not be the best way to solve this problem, but it fits with
what you have done so far. :-)

This is the most general case, which will give us 32 solutions. Now
let's add constraints according to the given information.

First let's translate, "If Mr. Johnson goes, his wife goes, too." We can
express that constraint in a predicate father_mother(Father,Mother).

father_mother(yes,yes).     %% If Father goes, so does Mother.
father_mother(no,_).        %% If Father does not go, Mother may or may
not.

And we add the constraint to go/5.

go(Grandpa,Father,Mother,Son,Daughter)  :-
father_mother(Father,Mother),
grandpa(Grandpa),
father(Father),
mother(Mother),
marcel(Son),
jacqueline(Daughter).

This gives us 24 valid possibilities.

Note that it's more efficient to put the constraint first.

Now we can add the rest of the constraints.

Good luck!

Bill
```
 0
Bill
12/6/2004 8:23:53 PM
```Hello,

thank you for your help. I created now a working solution. Can you tell
me, if this solution is written in good PROLOG style or not. If it is
not, what can be done better ?

possibility(yes).
possibility(no).

possibilities([]).
possibilities([P|Ps]):-
possibility(P),
possibilities(Ps).

implies(A,B) :-
((A=3Dyes,B=3Dyes);A=3Dno).

validparty([Mr,Mrs,Marcel,Jacqu,Grandpa]):-
possibilities([Mr,Mrs,Marcel,Jacqu,Grandpa]),
(Marcel=3Dyes;Jacqu=3Dyes),
implies(Mr,Mrs),
implies(Marcel,Grandpa),
implies(Mrs,\+Grandpa),
implies(Jacqu,Mr),
implies(Jacqu,Marcel).

Greetzt
Andr=E9

```
 0
ia
12/7/2004 12:47:23 AM