prolog

Why is my predicate not backtracking?

可紊 提交于 2019-12-23 18:38:20
问题 I don't understand why my predicate doesnt backtrack and find all solutions. person(john). person(erik). allExceptSpider(person(Spider),T ):- setof(person(X),person(X),S), subtract(S,[person(Spider) ],T). If I call this predicate with Two variables: allExceptSpider(person(Z),Q) Then it will only give me the answer Z = john, Q = [person(erik)] but it won't backtrack to find Z = erik ,Q = [person(john)] why? 回答1: TL;DR: If you use subtract/3 , your code may lose logical-purity. person(john).

prolog appending a list and a atom

淺唱寂寞╮ 提交于 2019-12-23 18:07:58
问题 I'm quite new to prolog, and I tried the following: | ?- append([1],2,R). R = [1|2] yes I have no idea what the notation [1 | 2] means. Trying to search this was painful, and I couldn't find any thing. I guess this is something like the lisp (1 . 2) resulted from (cons 1 2) . Can someone explain/reference to explanations about this? If it matters to anyone, I'm using GNU-prolog version 1.3.0 回答1: The "General" form of [H|T] The list notation [H|T] is a syntactic sugar for '.'(H,T) which is

All combinations of a list without doubles in Prolog

 ̄綄美尐妖づ 提交于 2019-12-23 18:05:38
问题 Is there a simple way of getting all the combinations of a list without doubles. Without doubles I mean also no permutations of each other. So no [a,b,c] and [c,a,b] or [c,b,a] . So for the input [a,b,c] the output would be: [a] [b] [c] [a,b] [a,c] [b,c] [a,b,c] I can only find solutions WITH the doubles (permutations) 回答1: The solution to this problem is rather simple: there is evidently only one combination out of the empty set: the empty set: combs([],[]). Furthermore for each element, you

Why does Prolog wait

邮差的信 提交于 2019-12-23 18:04:29
问题 So I'm doing a work for my university @prolog, and I'm trying to find a answer to the following practice: find if they're a common element between the two lists. I wrote: inlist(X,[X|_]). inlist(X,[H|L]) :- inlist(X,L). isOneInterest([X],[X|_]). isOneInterest([X|L1],L) :- ( inlist(X,L) ; isOneInterest(L1,L) ). Now I know there are better solutions, and I will see them gladly but my question is: Why does Prolog wait after answering true ? When answering false , it doesn't wait. example: 11 ?-

Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number

混江龙づ霸主 提交于 2019-12-23 18:04:17
问题 :- use_module(library(clpfd)). % load constraint library % [constraint] Compute a list of distinct odd numbers (if one exists), such that their sum is equal to a given number. odd(Num) :- Num mod 2 #= 1. sumOfList([],N,N) :- !. sumOfList([H|T],Counter,N) :- NewN #= H + Counter, sumOfList(T,NewN,N). buildOddList(N,InputList,L) :- %return list when sum of list is N V in 1..N, odd(V), append(InputList,[V],TempL), sumOfList(TempL,0,N)-> L = TempL; buildOddList(N,TempL,L). computeOddList(N) :-

Prolog convert mins to hours

谁说我不能喝 提交于 2019-12-23 16:56:43
问题 This is the code I have created. mins_to_hours(In, H, M):- In < 60, H = 0, M is In. mins_to_hours(In, H, M):- In >= 60, H is H1+1, In1 is In-60, mins_to_hours(In1, H1, M). It works fine when the minutes are less than 60, e.g. ?- mins_to_hours(20,H,M). H = 0, M = 20 ; false. However when trying to run it for more than 60 minutes ?- mins_to_hours(80,H,M). it outputs an exception ERROR: Arguments are not sufficiently instantiated ERROR: In: ERROR: [9] _3198 is _3204+1 ERROR: [8] mins_to_hours(80

What is a “Test succeeded with choicepoint” warning in PL-Unit, and how do I fix it?

淺唱寂寞╮ 提交于 2019-12-23 16:13:04
问题 I'm writing a prolog program to check if a variable is an integer . The way I'm "returning" the result is strange, but I don't think it's important for answering my question. The Tests I've written passing unit tests for this behaviour; here they are... foo_test.pl :- begin_tests('foo'). :- consult('foo'). test('that_1_is_recognised_as_int') :- count_ints(1, 1). test('that_atom_is_not_recognised_as_int') :- count_ints(arbitrary, 0). :- end_tests('foo'). :- run_tests. The Code And here's the

Structuring SWI-Prolog Code into Modules for Unit Testing for Varying Data Sets and Module Implementations

对着背影说爱祢 提交于 2019-12-23 16:04:02
问题 To elaborate on a discussion in the comments below my last question: I am looking for suggestions on techniques or best practices for structuring SWI-Prolog code in order to be able to use and test alternative, interchangeable implementations of algorithms and their supporting modules. The current situation can be illustrated using the following small, ficticous example: The user provides some input data (file data.pl ) and loads a module with an algorithm to be applied (file graph.pl ). The

Is it possible to reverse a list with only two arguments?

蓝咒 提交于 2019-12-23 16:01:02
问题 Is it possible to reverse a list in Prolog with only two arguments? Such as: reverse_list(List, Reversed). This is not homework, I'm reading Seven Programming Languages in Seven Weeks and I got curious. With three arguments you could use an accumulator (much like in functional programming): reverseList([], Accumulator, Accumulator). reverseList([Head|Tail], Accumulator, Solution) :- reverseList(Tail, [Head|Accumulator], Solution). reverseList(List, Solution) :- reverseList(List, [], Solution)

Solve Instant Insanity in PROLOG with CLP

不羁的心 提交于 2019-12-23 15:51:54
问题 This is the game I've managed to generate the problem with 4 colours and 4 cubes randomly mixed and following the colour scheme suggested in the link. So, the goal is to generate the possible solutions to the problem using clpfd . The main principle is basic, the same face for all 4 cubes must be different. Used all_different/2 on 4 lists, each of them containing the respective side of the "tower" composed by 4 faces. So far, so good. Now I must assure that the final result is a composition