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

Prolog union for A U B U C

union(A, B, C, U) :- union(A, B, V), union(C, V, U). Your definition of union/3 can be improved by replacing … not(element(X,L)), … by … maplist(dif(X),L), … or … non_member(X, L), …. non_member(_X, []). non_member(X, [E|Es]) :- dif(X, E), non_member(X, Es). Here is a case where the difference shows: ?- union([A],[B],[C,D]). A = C, B … Read more

prolog change show answer to print into list

When describing a list, always consider using DCGs. In your case, you can very easily obtain what you want with a few simple modifications to your code: show_result(Squares,MaxRow,MaxCol, List) :- phrase(show_result(Squares,MaxRow,MaxCol,1), List). show_result(_,MaxRow,_,Row) –> { Row > MaxRow }, !. show_result(Squares,MaxRow,MaxCol,Row) –> { phrase(show_result(Squares,MaxRow,MaxCol,Row,1), Line) } , [Line], { Row1 is Row+1 }, show_result(Squares,MaxRow,MaxCol,Row1). show_result(_,_,MaxCol,_,Col) … Read more