Prolog, "or" operator for rule statments -


i new prolog , want make rule statement whether 2 people cousins.

this current code, added "or" need or operator:

cousins(e, f) :- siblings(a, c) or siblings(a, d) or siblings(b, c) or siblings (b, d), parent(a, e), parent(b, e), parent(c, f), parent(d, f).

i need 1 of siblings() pass, parent() must pass.

in prolog, "or" operator ;. or can achieved having different clauses predicate.

let's see happens if first alternative turns out hold:

cousins(e,f):- siblings(a, c),    parent(a, e), parent(b, e), parent(c, f), parent(d, f). 

or, happens if 2nd holds?

cousins(e, f) :- siblings(a, d),    parent(a, e), parent(b, e), parent(c, f), parent(d, f). 

similar 3rd , 4th:

cousins(e, f) :- siblings(b, c),    parent(a, e), parent(b, e), parent(c, f), parent(d, f).  cousins(e, f) :- siblings (b, d),    parent(a, e), parent(b, e), parent(c, f), parent(d, f). 

now have "or" condition expressed 4 clauses.

but must have left many important details out. wanted have 2 pairs of parents, need add inequalities: parent(a, e), parent(b, e), \= b etc. then, new 2nd clause copy of 1st, variables renaming; same 3rd , 4th clauses. it'll suffice leave 1 of each pair.

but why need know both parents of person? don't, really. matter, if it's mother or father? doesn't. so, in end, 1 clause sufficient:

cousins(e,f):- siblings(a, c), parent(a, e), parent(c, f). 

you still have check degenerate cases, won't ever end proclaiming person own cousin.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -