Prolog find all subsets matching condition -
i'm trying solve problem in swi-prolog. have list of suitable elements (constants) obtained with.
suitables(l) :- setof(x, issuitable(x), l).
each element above has score via functor, , need subsets have score > 10. know how score sum:
scoresum([], 0). scoresum([h,t], tot) :- getscore(h,f),scoresum(t, rest), tot f+rest.
and status can expressed this:
cond(l) :- scoresum(l, r), r > 10.
how subsets matching given condition? can subsets based on reply here, how iterate on result subsets matching condition?
provided scoresum/2
starts scoresum([h|t], tot) :- ...
seq_subseq([], []). seq_subseq([_|es], fs) :- seq_subseq(es, fs). seq_subseq([e|es], [e|fs]) :- seq_subseq(es, fs). possiblesubset(s) :- suitables(l), seq_subseq(l, s), cond(s). ?- setof(s, possiblesubset(s), subs). ?- setof(s, (suitables(l), seq_subseq(l,s), cond(s)), subs). % no l^ needed ; there 1 l.
however, rather unusual represent such sets explicitly, because prolog can generate them efficiently.
prolog
No comments:
Post a Comment