Love  programming
Love programming

Reputation: 55

filter sublist in prolog

I want to use a predicate to find the sublist meet the requirement that contains at least one atomic(not variable).

contain_atomic([[a,c,e],[a,e,D],[D,A,E]],X).
X = [[a,c,e],[a,e,D]].

So far, I can only turn those sublist whose elements are all variables to empty.

all_variable([],[]).
all_variable([A|B],[]):-
    \+atomic(A),
    all_variable(B,[]).

And what's next? Am I close to success?

Upvotes: 0

Views: 175

Answers (1)

mat
mat

Reputation: 40768

I recommend you write a predicate that states what must hold for a single sublist to be included in the result (I leave this task as an easy exercise for your concrete case):

your_property(Sublist) :-
    ...

Then, use include/3 to filter all sublists that satisfy this property:

include(your_property, Lists0, Lists)

Always (at least) consider using include/3 and exclude/3 when filtering lists, because they simplify the task by only necessitating a description of the property for a single element.

Upvotes: 1

Related Questions