Re: how to refractor this ?
Roelof Wobben <[email protected]>
| Newsgroups | gmane.comp.ai.prolog.swi |
|---|---|
| Message-ID | <[email protected]> |
I made my gene to this :
male(roelof).
male(mans).
male(ronald).
male(jan).
female(chantal).
female(marie).
female(gerda).
female(dagmar).
female(denise).
female(kimberly).
% parent (X,Y) means that X is a parent of Y.
parent(mans,gerda).
parent(mans,roelof).
parent(marie,gerda).
parent(marie,roelof).
parent(dagmar,denise).
parent(dagmar,kimberly).
parent(ronald,denise).
parent(ronald,kimberly).
parent(chantal,tamara).
parent(roelof,tamara).
parent(jan,chantal).
parent(jan,dagmar).
% checks if a Child has a father.
father_child(Father, Child) :-
parent(Father, Child),
male(Father).
%checks if a Child has a mother.
mother_child(Mother, Child) :-
parent(Mother, Child),
female(Mother).
%checks if two children (child, sibling) have the same father.
same_father(Child, Sibling) :-
father_child(Father, Child),
father_child(Father, Sibling).
%checks if two children (child, sibling) have the same mother.
same_mother(Child, Sibling) :-
mother_child(Mother, Child),
mother_child(Mother, Sibling).
%checks if a Child has a sibling.
siblings(Child, Sibling) :-
same_father(Child, Sibling),
Child \== Sibling ;
same_mother(Child, Sibling),
\+ same_father(Child, Sibling).
%display the names of the siblings of a child. Works with one unknown and both unknown.
display_siblings(X,Y):-
setof(X-Y, (siblings(X,Y), X @< Y), Sibs),
write(Sibs).
%checks if a child has a brother.
brother_child(Child, Brother) :-
siblings(Child, Brother),
male(Brother).
%displays the name of a brother of a child.
display_brother(X,Y) :-
setof(X-Y, (brother_child(X,Y), X @< Y),Brother),
write(Brother).
%checks if a child has a sister.
sister(Person, Sister) :-
siblings(Child, Sister),
female(Sister).
%displays the name of the sister of a child. Works with (x,y) and (child,X).
display_sisters(Person) :-
write('% sisters of '), write(Person), nl,
( sister(Person, Sister),
portray_clause(sister(Person, Sister)),
fail
; true
),
write('% end'), nl.
but this does not work.
When I do :
display_sister(X) I see this output:
?- display_sisters(X).
% sisters of _G817
sister(_, gerda).
sister(_, kimberly).
sister(_, denise).
sister(_, dagmar).
sister(_, chantal).
% end
So I miss the name of which person the second name is a sister.
And I see that for example sister(_, kimberly) and (_,denise) this is a duplicate which I try to avoid.
When I do this:
display_sisters(gerda) I see this :
?- display_sisters(gerda).
% sisters of gerda
sister(gerda, gerda).
sister(gerda, kimberly).
sister(gerda, denise).
sister(gerda, dagmar).
sister(gerda, chantal).
Which is not right. Gerda has no sisters. She is not a sister of her own. Kimberly. Denise and Dagmar are for certain
no sisters. Dagmar is a sister of my wife and Kimberly and Denise are her children.
Chantal is my wife. So she would be a sister-in-law.
Roelof
-------------- next part --------------
HTML attachment scrubbed and removed