recursion - Prolog- family tree of children -
if wanted recursively go down family tree in prolog , return children of every branch, how start that?
thanks
assume following tree, children in red
a simple solution be:
male(lennart). male(mike). male(donald). male(usain). male(joar). male(adam). male(dan). male(simon). female(hillary). female(elise). female(lisa). female(lena). parent(mike, lennart). parent(mike, lena). parent(lennart, donald). parent(lennart, hillary). parent(lennart, usain). parent(lena, adam). parent(lena, simon). parent(adam, dan). parent(donald, lisa). parent(hillary, joar). parent(hillary, elise). child(lisa). child(joar). child(elise). child(dan). child(simon). %% predicate rules father(x,y) :- male(x),parent(x,y). mother(x,y) :- female(x),parent(x,y). son(x,y) :- male(x),parent(y,x). daughter(x,y) :- female(x),parent(y,x). family_children(x, x):- child(x). family_children(x, child):- parent(x,y), family_children(y, child).
test run:
[debug] ?- family_children(mike, child). child = lisa ; child = joar ; child = elise ; child = dan ; child = simon ; false.
this quick example of how done, example relies in assumption child cannot have child, solution improved , can add rules cousin/2
, grandfather/2
, sister/2
, uncle/2
etc...
hope got hang of idea now, luck.
Comments
Post a Comment