prolog-toplevel

SWI-Prolog - show long list

百般思念 提交于 2019-11-27 05:14:55
I'm using SWI-Prolog and I'm trying to print a list but if the list has more than 9 items - it look like that - [1, 15, 8, 22, 5, 19, 12, 25, 3|...] is there a way to show the whole list? Have a look at: http://www.swi-prolog.org/FAQ/AllOutput.html The simple solution is to type w after the answer is given, i.e.: ?- n_queens_problem(10,X). X = [1, 3, 6, 8, 10, 5, 9, 2, 4|...] [write] X = [1, 3, 6, 8, 10, 5, 9, 2, 4, 7] After you have pressed the "w"-key "[write]" is displayed at the end and the full solution appears in the next line. I've found two ways. 1. ?- set_prolog_flag(answer_write

Implementing “last” in Prolog

冷暖自知 提交于 2019-11-26 11:37:35
问题 I am trying to get a feel for Prolog programming by going through Ulle Endriss\' lecture notes. When my solution to an exercise does not behave as expected, I find it difficult to give a good explanation. I think this has to do with my shaky understanding of the way Prolog evaluates expressions. Exercise 2.6 on page 20 calls for a recursive implementation of a predicate last1 which behaves like the built-in predicate last . My attempt is as follows: last1([_ | Rest], Last) :- last1(Rest, Last

Definition of Reflexive Transitive Closure

我只是一个虾纸丫 提交于 2019-11-25 22:45:22
问题 Many predicates essentially use some form of transitive closure, only to discover that termination has to be addressed too. Why not solve this once and forever with closure0/3 : :- meta_predicate closure0(2,?,?). :- meta_predicate closure(2,?,?). :- meta_predicate closure0(2,?,?,+). % internal closure0(R_2, X0,X) :- closure0(R_2, X0,X, [X0]). closure(R_2, X0,X) :- call(R_2, X0,X1), closure0(R_2, X1,X, [X1,X0]). closure0(_R_2, X,X, _). closure0(R_2, X0,X, Xs) :- call(R_2, X0,X1), non_member(X1