Advertisement

Prolog problem

Started by October 04, 2004 10:27 AM
1 comment, last by capn_midnight 20 years, 1 month ago

parent(b, a).
parent(c, a).
parent(d, a).
parent(e, b).
parent(f, b).
parent(h, d).
parent(i, d).
parent(j, d).
parent(k, d).

child(X, Y) :-
    parent(Y, X).

grandparent(X, Y) :-
    parent(X, Z),
    parent(Z, Y).

grandchild(X, Y) :- 
    child(X, Z),
    child(Z, Y).

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

auncle(X, Y) :-
    parent(X, Z),
    sibling(Z, Y).

cousin(X, Y) :-
    auncle(X, Z),
    child(Y, Z).

happy(X) :-
    not(parent(Y,X)).

The procedure "happy" works if you have a specific unit in mind, i.e. happy(a) returns no and happy(c) returns yes (happy defined as not having kids). However, I can't seem to figure out how to list ALL happy units, ala happy(X).

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

You should be able to do findall(Person, happy(Person), ListOfHappyPeople).
Advertisement
apparently the answer was:

happy(X) :-
parent(X, _),
not(parent(_, X)).

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement