family tree - determine the old line of succession prolog -


given british royal family, using male(x),female(x) , parent(x,y) parent of x y, how create line of succession(x,y) y successor of x in prolog

i have defined males , female , parent

i have tried well:

son(x,y) :- parent(x,y), male(y). daughter(x,y) :- parent(x,y), female(y). successor(x,y):- (son(x,z);daughter(x,z)) , (y=z;successor(z,y)). 

apparently work before anne after louise, should go anne not peter...

i have arrange facts according birth in parent(x,y). facts:

male(charles). male(william). male(peter). male(henry). male(andrew). male(edward). male(viscount). male(savanna).  female(elizabeth). female(anne). female(zara). female(beatrice). female(eugenie). female(louise). female(isla).  parent(elizabeth,charles). parent(elizabeth,anne). parent(elizabeth,andrew). parent(elizabeth,edward).  parent(anne,peter). parent(anne,zara). parent(charles,william). parent(charles,henry). parent(andrew,beatrice). parent(andrew,eugenie). parent(edward,louise). parent(edward,viscount). parent(peter,savanna). parent(peter,isla). 

results have gotten far when querying successor(x,y).

x = elizabeth, y = charles ; x = elizabeth, y = william ; x = elizabeth, y = henry ; x = elizabeth, y = andrew ; x = elizabeth, y = beatrice ; x = elizabeth, y = eugenie ; x = elizabeth, y = edward ; x = elizabeth, y = viscount ; x = elizabeth, y = louise ; 

after point, when try go anne family goes wrong.

x = anne, y = peter ; x = anne, y = savanna ; x = anne, y = isla ; x = charles, y = william ; x = charles, y = henry ; x = edward, y = viscount ; x = peter, y = savanna ; x = elizabeth, y = anne ; x = elizabeth, y = peter ; x = elizabeth, y = savanna ; x = elizabeth, y = isla ; x = elizabeth, y = zara ; x = anne, y = zara ; x = andrew, y = beatrice ; x = andrew, y = eugenie ; x = edward, y = louise ; x = peter, y = isla ; 

the desired output when going anne tree want is

    x = elizabeth,     y = anne;     x = anne,     y = peter ;     x = anne,     y = savanna ;     x = anne,     y = isla ; 

i have been trying sorts of combinations , closest got far x parent while y child in parent(x,y).

tried combinations of successor:

successor(x,y):- (son(x,z);daughter(x,z)) , (y=z;successor(z,y)).  successor(x,y):- parent(x,z), (y=z ; successor(z,y)). successor(x,y):- (male(z);female(z)) , (y=z;successor(z,y)). successor(x,y):- (parent(x,z),(male(z);female(z)), (y=z;successor(z,y)). 

neither of them worked.

the actual result show old line of succession charles' family, andrew's family , edward's family , lastly anne's family.

your problem use condition more complex needed, , database not complete.

specifically, charles , peter miss gender, code show

?- forall(((parent(p,_) ; parent(p,_)), \+(male(p);female(p))), writeln(p)). charles charles peter peter charles charles peter peter true. 

son , daughter depend on gender, , make condition fail. correct database, or stick simpler:

successor(x,y) :- parent(x,z), (y=z ; successor(z,y)). 

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 -