Collect all “minimum” solutions from a predicate

What is the idiomatic approach to this class of problems? Is there a way to simplify the problem? Many of the following remarks could be added to many programs here on SO. Imperative names Every time, you write an imperative name for something that is a relation you will reduce your understanding of relations. Not … Read more

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].

Most general higher-order constraint describing a sequence of integers ordered with respect to a relation

Hoogle was not very useful, but Hayoo is! foldcmpl so this is a special form of fold for a list, but it does not apply length list times but one time less. isSortedBy is not entirely general in its name, but in its signature. Maybe insisting on the most general name is not that helpful. … Read more

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

tech