Prolog - Array of arrays, returning entries where array length is greater than one

Viewed 93

I am very new to logical programming.

The purpose of this program is to return a list of all sibling groups. I am currently organizing parent/child groups like so:

/* Where Banana is the parent */
child_of(Apple, Banana).
child_of(Grape, Banana).
child_of(Pear, Banana).
child_of(Apricot, Pear).

display_siblings(X) :-
    findall(Siblings,
           bagof(
                C,
                child_of(_, C), Siblings), 
                X).

This not only returns all sibling groups, but seems to return all family groups in which only one child is available.

For example:

[[Apple, Grape, Pear], [Apricot]]

My first idea was to use this predicate:

not_just_one([X,Y]]) :-
    length(X, len),
    len \= 1,
    not_just_one(Y).

Which gives me the following error: Illegal start of term

How can I change display_siblings to only allow sibling pairs in which more than one child exists?

1 Answers

My predicate ended up looking like this:

yokatta(C,Siblings) :-
    bagof(C, child_of(_, C), Siblings),
    length(Siblings, X),
    X > 1.

display_siblings(Ss) :-
    findall(Siblings, yokatta(C,Siblings), Ss).

The reason this works is because it abstracts each list and then isolates the results to only results with a length score greater than 1.

Related