Prolog: Filtering a list?

SWI-Prolog offers exclude/3 and other such meta-predicates. Your original problem can be coded like this: are_identical(X, Y) :- X == Y. filterList(A, In, Out) :- exclude(are_identical(A), In, Out). Usage example: ?- filterList(A, [A, B, A, C, D, A], Out). Out = [B, C, D].

Definition of Reflexive Transitive Closure

It’s useful, but in my opinion not yet ideal because I cannot cut duplicate paths at the point of their creation. Consider, with the complete graph K_n: n_complete(N, Es) :- numlist(1, N, Ns), phrase(pairs(Ns), Es). adjacent(Edges, X, Y) :- member(edge(X, Y), Edges). pairs([]) –> []. pairs([N|Ns]) –> edges(Ns, N), pairs(Ns). edges([], _) –> []. edges([N|Ns], … Read more

Prolog map procedure that applies predicate to list elements

This is usually called maplist/3 and is part of the Prolog prologue. Note the different argument order! :- meta_predicate(maplist(2, ?, ?)). maplist(_C_2, [], []). maplist( C_2, [X|Xs], [Y|Ys]) :- call(C_2, X, Y), maplist( C_2, Xs, Ys). The different argument order permits you to easily nest several maplist-goals. ?- maplist(maplist(test),[[1,2],[3,4]],Rss). Rss = [[1,4],[9,16]]. maplist comes in … Read more