prolog

Display a list in its 'raw' ./2 format

时光怂恿深爱的人放手 提交于 2019-12-30 22:54:16
问题 Is it possible to display a Prolog list in its ./2 format, e.g. for the list: | ?- L=[a,b,c]. L = [a,b,c] ? yes Is there a means to display: L = .(a, .(b, .(c, []))). 回答1: Normally, write_canonical(List) or ?- write_term(List, [quoted(true), ignore_ops(true)]) , as pointed out in the comments. Since SWI-Prolog decided to do things differently, this is not good enough: ?- write_canonical([a]). [a] true. ?- write_term([a], [quoted(true), ignore_ops(true)]). [a] true. ?- write_term([a],

Prolog insertion sort

蓝咒 提交于 2019-12-30 22:50:47
问题 There is a simple Prolog insertion sort alghoritm: sorting([A|B], Sorted) :- sorting(B, SortedTail), insert(A, SortedTail, Sorted). sorting([], []). insert(A, [B|C], [B|D]) :- A @> B, !, insert(A, C, D). insert(A, C, [A|C]). It does well on normal lists: ?- sorting([5, 4, 9, 1, 3, 8], X). X = [1, 3, 4, 5, 8, 9]. But I also need to sort sublist of list contains any of them: ?- sorting([2, 5, [5, 4, 3], [6, 3], 4, 8], X). X = [2, 4, 5, 8, [5, 4, 3], [6, 3]]. Is what return now. And ?- sorting(

Use cut in Prolog to define a once_member/2 function

和自甴很熟 提交于 2019-12-30 18:26:36
问题 Disclaimer: This is informal and non-assessed coursework to do in my own time. I have tried it myself, failed and am now looking for some guidance. I am trying to implement a version of the member/2 function which will only return members for a list once. For example: | ?- member(X, [1,2,3,1]). X = 1 ? ; X = 2 ? ; X = 3 ? ; X = 1 ? ; I would like it to only print out each number a maximum of once. | ?- once_member(X, [1,2,3,1]). X = 1 ? ; X = 2 ? ; X = 3 ? ; no We have been told to do this

Prolog: how to write (and use) a function that lists all list permutations?

﹥>﹥吖頭↗ 提交于 2019-12-30 17:37:07
问题 I've found such an example of naive sort written in prolog and I am trying to understand it: naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted). is_sorted([]). is_sorted([_]). is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]). perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm). perm([],[]). delete(X,[X|T],T). delete(X,[H|T],[H|NT]):-delete(X,T,NT). Naive_sort call works correctly but I just can't figure out why. The main problem is the permutation. When it is called implicitly it

Rule to calculate power of a number when the exponent is Negative in Prolog?

我们两清 提交于 2019-12-30 12:36:24
问题 I have a power function pow that attempts to calculate the value of B to the power of E . So far I handle the cases- 1. exponent is 0 2. exponent is non-zero pow(B,0,1). pow(B,E,Result):- E2 is E - 1, pow(B,E2,Result2), Result is B*Result2. How can I add another case where the power function can handle negative exponents? 回答1: First, one should consider how to define 0 0 . Formally speaking it is indeterminate . It could be zero or it could be 1. As Wolfram's Mathworld says in its article on

Recursion in PROLOG?

杀马特。学长 韩版系。学妹 提交于 2019-12-30 11:07:33
问题 Given the following Prolog facts: f(a, [b]). f(b, [c]). f(c, [d]). f(d, [e]). f(e, []). I need to create a query xyz(a,Y) so that I get Y = [e,d,c,b] , since a depends on b, which depends on c, etc. My current query is: xyz(X,Y):- f(X,P), member(Y,[P]). However, this query for xyz(a,Y) only gives me Y = [b] , and not b's dependents, etc. I figured that perhaps I can add these two lines to the end of the query above, but that doesn't work as I would like it to. Since the previous free query

aggregate/3 in swi-prolog

≡放荡痞女 提交于 2019-12-30 08:14:56
问题 I need to count all X , that some_predicate(X) and there really a lot of such X . What is the best way to do that? First clue is to find it all, accumulate to a list and return it length. countAllStuff( X ) :- findall( Y , permutation( [1,2,3,4,5,6,7,8,9,10], Y ) , List ), length( List, X ). ( permutation/2 is only example showing that there are many variants and it's bad way to collect it all) Obviously, I have stack-overflow. ?- countAllStuff( X ). ERROR: Out of global stack Than, I'm

aggregate/3 in swi-prolog

江枫思渺然 提交于 2019-12-30 08:14:09
问题 I need to count all X , that some_predicate(X) and there really a lot of such X . What is the best way to do that? First clue is to find it all, accumulate to a list and return it length. countAllStuff( X ) :- findall( Y , permutation( [1,2,3,4,5,6,7,8,9,10], Y ) , List ), length( List, X ). ( permutation/2 is only example showing that there are many variants and it's bad way to collect it all) Obviously, I have stack-overflow. ?- countAllStuff( X ). ERROR: Out of global stack Than, I'm

How to expand a resulting list in SWI-Prolog?

旧时模样 提交于 2019-12-30 07:04:31
问题 ?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. 回答1: There is a limit on the depth to prevent too long output. You can change it with set_prolog_flag/1. ?- length(L, 25). L = [_G257, _G260, _G263, _G266, _G269, _G272, _G275, _G278, _G281|...]. ?- current_prolog_flag(toplevel_print_options, V). V = [quoted(true), portray(true),

How to expand a resulting list in SWI-Prolog?

别等时光非礼了梦想. 提交于 2019-12-30 07:04:09
问题 ?- length(L,25). L = [_G245, _G248, _G251, _G254, _G257, _G260, _G263, _G266, _G 269|...]. If I use write(L) following the length predicate then the interpreter prints the list twice, one expanded and the other not. 回答1: There is a limit on the depth to prevent too long output. You can change it with set_prolog_flag/1. ?- length(L, 25). L = [_G257, _G260, _G263, _G266, _G269, _G272, _G275, _G278, _G281|...]. ?- current_prolog_flag(toplevel_print_options, V). V = [quoted(true), portray(true),