3 Replies - 723 Views - Last Post: 07 October 2012 - 06:03 PM Rate Topic: -----

#1 Ap0C552  Icon User is offline

  • D.I.C Regular

Reputation: -7
  • View blog
  • Posts: 318
  • Joined: 08-December 10

prolog rule problem

Posted 04 October 2012 - 09:30 AM

I am creating rules that establish family relationships.

I have the rule...

brother(X,Y):-(parent(Z,X),parent(Z,Y)),X\==Y,male(X).


But the problem is that it returns duplicates.

So if john has a brother joe, it will say john has two brothers named joe.

because it checks that they share a father and returns joe, and checks that they have the same mother and returns joe.


But of course I want it to work if they only have one parent in common as well.

Can anyone suggest me in the right direction?

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: prolog rule problem

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,747
  • Joined: 19-February 09

Re: prolog rule problem

Posted 05 October 2012 - 09:03 PM

You can use a cut ! to say that the current choice is the correct one, and to disallow backtracking.

	brother(X,Y):- parent(Z,X), parent(Z,Y), 
                       X\==Y, male(X) , !.


Was This Post Helpful? 0
  • +
  • -

#3 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2134
  • View blog
  • Posts: 3,272
  • Joined: 21-June 11

Re: prolog rule problem

Posted 06 October 2012 - 02:24 AM

View Post#define, on 06 October 2012 - 06:03 AM, said:

You can use a cut ! to say that the current choice is the correct one, and to disallow backtracking.


But that will only return one brother even if there really are multiple brothers.
Was This Post Helpful? 1
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1371
  • View blog
  • Posts: 4,747
  • Joined: 19-February 09

Re: prolog rule problem

Posted 07 October 2012 - 06:03 PM

Thanks sepp2k that's right.

Another predicate may help something like :

parents(M, F, C) :-



that defines both parents of the child.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1